Generally, Bitwise operators in C# language e.g. &, | (Bitwise AND ,Bitwise OR , Bitwise Left Shift ,Bitwise Right Shift,Bitwise XOR respectively) are used to perform binary operations of an expression.
Binary numbers are represented as 0 and 1.
For Example 3 is the decimal number, then binary number of 3 is 11, i.e 2+1.
For Example 253 is the decimal number then binary number of 253 is 11101011 , i.e, 128+64+32+8+2+1
for a 8 bit binary representation starts pow(2,0) to pow(2,7) as sequence as shown below.
pow(2,7) pow(2,6),pow(2,5) pow(2,4),pow(2,3) pow(2,2),pow(2,1) pow(2,0) --binary number sequecnce
128 64 32 16 8 4 2 1
1 1 1 0 1 0 1 1 -- binary sequence representation of decimanumber 253
i.e 1*128+1*64+1*32+0*16+1*8+0*4+1*2+1*1
Bitwise Operators
- AND
- OR
- SHIFT
- XOR
Bitwise & Operator
Truth table for AND operation
A B AND
0 (false) 0 (false) 0 (false)
1(True) 0 (false) 0 (false)
0 (false) 1(True) 0 (false)
1(True) 1(True) 1(True)
AND operation results to true only if both values are true.
Code Example on & (AND) Bitwise operator:
Read below program first where value of a is 10 and value of variable b is 20.
since the value of a is 10, then binary represent of value 10 is 00001010
since the value of b is 20, then binary represent of value 20 is 000110
the bitwise AND operation of 10 AND 20 is 0 as per above given Truth table for AND operation.
Consider value of a is 10 and value of b is 10 , then bitwise AND operation of 10 and 10 is 10 as per given Truth for AND operation
C# code
using System;
class Sample
{
public static void Main()
{
byte A = 10;// binary equivalent for 10 is 01010
byte B = 20;// binary equivalent for 20 is 10100
long result = A & B; // AND operation result should be 00000
Console.WriteLine("Binary AND Operation {0} AND {1} Result :{2}", A, B, result);
A = 10;// binary equivalent for 10 is 01010
B = 10;// binary equivalent for 10 is 01010
result = A & B; // AND operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("Binary AND {0} AND {1} Result : {2}", A, B, result);
}
}
Output:
Binary AND Operation 10 AND 20 Result :0
Binary AND Operation 10 AND 10 Result : 10
Bitwise | Operator
Truth table for OR operation
A B AND
0 (false) 0 (false) 0 (false)
1(True) 0 (false) 1 (false)
0 (false) 1(True) 1 (false)
1(True) 1(True) 1(True)
OR operation results to false only if both values are false. In all other cases OR operation is true.
Read below program first where value of a is 10 and value of variable b is 20.
since the value of a is 10, then binary represent of value 10 is 00001010
since the value of b is 20, then binary represent of value 20 is 000110
the bitwise OR operation of 10 AND 20 is 30 as per above given Truth table for OR operation.
Consider value of a is 10 and value of b is 10 , then bitwise OR operation of 10 and 10 is 10 as per given Truth for OR operation
Code Example on | (OR) Bitwise operator:
C# code
using System;
class Sample
{
public static void Main()
{
byte A = 10;// binary equivalent for 10 is 01010
byte B = 20;// binary equivalent for 20 is 10100
long result = A | B; // OR operation result should be 11110
Console.WriteLine("Binary OR Operation {0} AND {1} Result :{2}", A, B, result);
A = 10;// binary equivalent for 10 is 01010
B = 10;// binary equivalent for 10 is 01010
result = A | B; // OR operation result should be 01010
//so the result will contain 10 in decimal
Console.WriteLine("Binary OR Operation {0} AND {1} Result : {2}", A, B, result);
}
}
Output
Binary OR Operation 10 AND 20 Result :30
Binary OR Operation 10 AND 10 Result : 10
Shift Operators
- << –Left Shift Operator — performs multiplication operation with 2
- >> — Right Shift Operator– division operation with 2.
<< –Left Shift Operator
When Left Shift operations are done on a binary value the bits are shifted to one position towards left side and fill the right most bit with 0.
For Example,
consider value of A is 10 then binary value of A is 00001010.
Left shift operation 10 is 20 i.e if we shift one bit from leftmost and fill the right most bit with 0. 00001010 when shifted to left one position and filled with 0 in right most positions , then its value will be 00010100
C# code
C# code
using System;
class Sample
{
public static void Main()
{
byte A = 10;// binary equivalent for 10 is 01010
long result = A << 1; // Left Shift operation result should be 10100
//so the result will contain 20 in decimal
Console.WriteLine("{0} is Left Shifted to 1 position Result :{1}", A, result);
}
}
Output
10 is Left Shifted to 1 position Result :20
>> — Right Shift Operator
When Right Shift operations are done on a binary value the bits are shifted to one position towards Right side.
1010 when shifted to right one position its value will be 0101
Right shift operation 10 is 5.
C# code
using System;
class Sample
{
public static void Main()
{
byte A = 10;// binary equivalent for 10 is 01010
long result = A >> 1; // Right Shift operation result should be 0101
//so the result will contain 5 in decimal
Console.WriteLine("{0} is Right Shifted to 1 position Result :{1}", A, result);
}
}
Output
10 is Right Shifted to 1 position Result :5