Understanding Bit Operator In C#

& (AND) Operator

Binary AND Operator copies a bit to the result, if it exists in both the operands also returns true, if and only if both the operands are true. Unary variant returns the address of its operand. Binary variant is the bitwise AND of two operands.

AB& (AND) Operator
000
000
100
100
111
111
000
010

| OR Operator

Binary OR Operator copies a bit, if it exists in either operand i.e. true, if one or both operand is
true, false; if both the operands are false .

AB| (OR) Operator
000
000
101
101
111
111
000
011

^ XOR 

Returns true, if and only if, one of the operands is true.

AB

^(XOR ) Operator

000
000
101
101
110
110
000
011

~ Complement operator
 
The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are given below.  

A~
01
01
10
10
10
10
01
01

<< Binary Left Shift Operator  

The number of bits specified by the right operand moves the left operands value left. The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an integer or a type that has a predefined implicit numeric conversion to int. Thus, for example, 37 << 3 is shifting the number 37 to the left by 3 places. Of course, we are working with the binary representation of 37. The binary form of 37 is 00100101. After sliding by 3 to the left, we are left with 00101. However, we need to add three 0's to complete the bit 00101000. 

>>
Binary Right Shift Operator

The number of bits specified by the right operand moves the left operands value right. The right-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an integer or a type that has a predefined implicit numeric conversion to the integer. For example, 37 >> 3 is shifting the number 37 to the right by 3 places. Of course, we are working with the binary representation of 37 The binary form of 37 is 00100101. After sliding by 3 to the left, we are left with 00100. However, we need to add three 0's to complete the bit 00000100. Hope, the tutorial was helpful. Happy coding.
Ebook Download
View all
Learn
View all