Illustrates the use of the bitwise operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_6.cs illustrates the use of
the bitwise operators
*/
public class Example3_6
{
public static void Main()
{
byte byte1 = 0x9a; // binary 10011010, decimal 154
byte byte2 = 0xdb; // binary 11011011, decimal 219
byte result;
System.Console.WriteLine("byte1 = " + byte1);
System.Console.WriteLine("byte2 = " + byte2);
// bitwise AND
result = (byte) (byte1 & byte2);
System.Console.WriteLine("byte1 & byte2 = " + result);
// bitwise OR
result = (byte) (byte1 | byte2);
System.Console.WriteLine("byte1 | byte2 = " + result);
// bitwise exclusive OR
result = (byte) (byte1 ^ byte2);
System.Console.WriteLine("byte1 ^ byte2 = " + result);
// bitwise NOT
result = (byte) ~byte1;
System.Console.WriteLine("~byte1 = " + result);
// left shift
result = (byte) (byte1 << 1);
System.Console.WriteLine("byte1 << 1 = " + result);
// right shift
result = (byte) (byte1 >> 1);
System.Console.WriteLine("byte1 >> 1 = " + result);
}
}
Related examples in the same category