Home » C Programming » Bitwise Operators in C Programming

Bitwise Operators in C Programming

In a C programming language Bitwise operator works on bits and perform bit-by-bit operation. There are following bitwise operators in C:-

Operator Description Example
& Binary AND Operator   a & b
| Binary OR Operator a| b
^ Binary XOR Operator  a^b
~ Binary One’s Complement Operator is unary ~a
<<  Binary Left Shift Operator a<<b
>>  Binary Right Shift Operator a>>b

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

               A in binary     = 0011 1100         (60)                     

              B in binary     = 0000 1101          (13)

Bitwise AND (&)

                      A      = 0011 1100         (60)                

          &         B      = 0000 1101          (13)

———————————————————

                                0000 1100           (12)

Bitwise OR (|)

                      A      = 0011 1100         (60)                

          |          B      = 0000 1101          (13)

———————————————————

                                0011 1101           (61)

Bitwise XOR (|)

                      A      = 0011 1100         (60)                

          ^         B      = 0000 1101          (13)

———————————————————

                                0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

i=16 //0001 0000

Example:

Binary Right Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

int i = 16;                    // 0001 0000

i = i>>2;                      // 0000 0100  (shift right 2 bits, last 2 bits will be lost, and 2 zero add in front)

Example

Output

Exapmle:  Write a program to demonstrate Bitwise operator.

OUTPUT

Managed by MochaHost - Fast & Easy WordPress Hosting