1 / 4
2 / 4
3 / 4
4 / 4

Tuesday, 11 April 2023

Bitwise Operators in C Language

 In C Language we have 6 Bitwise Operators. Those are listed below

  1. & Bitwise AND Operator
  2. | Bitwise OR Operator
  3. ~ Bitwise NOT Operator
  4. ^ Bitwise XOR Operator
  5. << Bitwise Left Shift Operator
  6. >> 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:-
int a = 7 ;//00000111
int b = 9 ;//00001001
____________________
  a&b = 1 ;//00000001





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:-

int a = 7 ;//00000111
int b = 9 ;//00001001
____________________
  a|b = 15 ;//00001111
  



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:-
int a = 7 ;//00000111
int b = 9 ;//00001001
____________________
  a^b = 14 ;//00001110


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.
int a = 7 ;//00000111
~ a = -8 ;//11111000


<<  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:-
int a = 7 ;//00000111
a<< 1 = 14 ;//00001110
a<< 2 = 28 ;//00011100






>> 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


Thank you Happy Coding💻☺


No comments:

Post a Comment

If you have any doubts please let me know