In C Language we have 6 Bitwise Operators. Those are listed below
- & Bitwise AND Operator
- | Bitwise OR Operator
- ~ Bitwise NOT Operator
- ^ Bitwise XOR Operator
- << Bitwise Left Shift Operator
- >> Bitwise Right Shift Operator
The Bitwise operator converts the Decimal value into a Binary number and then does the Operation at the bit level.
Truth Table of & | and ^
Look at the above table; here a and b are two Operands, when we use the Operator, the corresponding operation happens at the bit level.
& Bitwise AND Operator
When both operands are 1 then it returns 1 as a result if one of the operands is 0 then it will return 0 as result.
Example:-
| Bitwise OR Operator
When one of the operands is 1 then it returns 1 as a result if both operands are 0 then it will return 0 as result.
Example:-
^ Bitwise XOR Operator
When two operands are different then it returns 1 as a result and if both operands are the same then it will return 0 as result.
Example:-
~ Bitwise NOT Operator
This operator is also known as Binary One's Complement. It returns the result as 1 when the operand is 0 and returns 0 when the operand is 1.
<< Bitwise Left Shift Operator
It shifts to the left side bitwise and the empty space that remains on the right side will be placed with zero. See the below example for an understanding.
Example:-
>> Bitwise Right Shift Operator
It shifts to the right side bitwise and the empty space that remains on the left side will be placed with zero. See the below example for a better understanding.
Example:-
int a = 7 ;//00000111 a>> 1 = 3 ;//0000011 a>> 2 = 1 ;//00000001
Run the Code and see the result on the code compiler
Related Links
Give your feedback in the below comment box