Bitwise operators are used to perform calculations on the bits.
Here is the list of bitwise operators,
- "&" (Bitwise AND) – returns 1 (sets bit), if both bits are set
- "|" (Bitwise OR) – returns 1 (sets bit), if any or all bits are set
- "^" (Bitwise XOR) – returns 1 (sets bit), if only one bit is set (not both bits are set)
- "~" (Bitwise NOT) – returns one’s compliment of the operand, it’s a unary operator
- "<<" (Bitwise Left Shift) – moves the number of bits to the left
- ">>" (Bitwise Right Shift) – moves the number of bits to the right
Syntax:
Operand1 & Operand2
Operand1 | Operand2
Operand1 ^ Operand2
~Operand
Operand1 << Operand2
Operand1 >> Operand2
Example:
Input:
int a = 10;
int b = 3;
//operations
a & b = 2
a | b = 11
a ^ b = 9
~a = -11
a << 2 = 40
a >> 2 = 2
C# code to demonstrate example of bitwise operators
Output