Bitwise Operators In C Programming With Examples

Explain Bitwise Operator In C Programming With Example
Get Our Latest Interview Questions: Aptitude, Reasoning, English, GD, HR-PI

Bitwise Operator:
Bitwise operator are used to manipulate data at bit level.

C Provides 6 Bitwise Operators:
  1. Bitwise And Operator (&):
This operator work with the help of & operator, It copies a bit to the result if it exists in both operand.
Bitwise And Operator Example:
 a = 4
 b = 5
 if we use and operator in a & b, so Now =>
a = 4, so in binary 4 is => 00000100
b = 5, so in binary 5 is => 00000101
---------------------------------------------
                                        00000100 => This is 4
       Hence Result of a & b = 4


  2. Bitwise OR Operator (|):
This operator work with the help of | operator, It copies a bit if it exists in either operand. .
Bitwise OR Operator Example:
 a = 4
 b = 5
 if we use and operator in a | b, so Now =>
a = 4, so in binary 4 is => 00000100
b = 5, so in binary 5 is => 00000101
---------------------------------------------
                                        00000101 => This is5
       Hence Result of a | b = 5

3. Bitwise Exclusive OR Operator (/\):
This operator work with the help of /\ operator, It copies a bit if it is set in one operand but not both. . .
Bitwise Exclusive OR Operator Example:
 a = 4
 b = 5
 if we use and operator in a /\ b, so Now =>
a = 4, so in binary 4 is => 00000100
b = 5, so in binary 5 is => 00000101
---------------------------------------------
                                        00000001 => This is 4
       Hence Result of a /\ b =1

4. Bitwise NOT Operator (~):
This operator work with the help of ~ operator, Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. It changes on mode to off and off mode to on.

Bitwise NOT Operator Example:
 a = 5
 if we use and operator in a ~ , so Now =>

a = 5, so in binary 5 is => 00000101
        2's Complement  => 11111010
         00000101
                     +1 
---------------------------------------------
          00000110
       Hence Result of ~a =6

5. Bitwise Left Shift Operator(<<):
This operator is used to shift bits into left side. Here left operands value is moved left by the number of bits specified by the right operand. 
 Bitwise Left Shift Operator Example:
 a=5
a<<2 (Shift a bit into 2 bits left)
Here a = 5 so in binary => 00000101
  Now 1st Shit Change => 00001010
        2nd Shift Change => 00010100 => (20) Answer.


6. Bitwise Right Shift Operator(>>):
This operator is used to shift bits into right side. Here right operands value is moved left by the number of bits specified by the right operand. 
 Bitwise Right Shift Operator Example:
 a=5
a>>2 (Shift a bit into 2 bits right)
Here a = 5 so in binary => 00000101
  Now 1st Shit Change => 00000010
        2nd Shift Change => 00000001 => (1) Answer

 Enter Your Email Id To Get Our Programming, Web Development, Video Tutorial, Job Updates In Mail :

Labels: