Bitwise Operators

Bitwise Operators

  • & (bitwise AND)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • ~ (bitwise NOT)
  • << (left shift)
  • >> (right shift)

Bitwise AND

Operator : &

The result of AND is 1 only if both bits are 1

Truth Table

A B &
0 0 0
0 1 0
1 0 0
1 1 1

Example:

                            #include <stdio.h>

int main(){
    int a=3,b=5,c;
    c = a & b;
    printf("%d",c);
    return 0;
}
                            
                        

Output:

                            
1
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ $$ (5)_{10} = (101)_2 $$ $$ (11)_2 \& (101)_2 = (001)_2 = (1)_{10} $$

Bitwise OR

Operator : |

The result of OR is 1 if any of the two bits is 1

Truth Table

A B |
0 0 0
0 1 1
1 0 1
1 1 1

Example:

                            #include <stdio.h>

int main(){
    int a=3,b=5,c;
    c = a | b;
    printf("%d",c);
    return 0;
}
                            
                        

Output:

                            
7
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ $$ (5)_{10} = (101)_2 $$ $$ (11)_2 \| (101)_2 = (111)_2 = (7)_{10} $$

Bitwise XOR

Operator : ^

The result of XOR is 1 if the two bits are different

Truth Table

A B ^
0 0 0
0 1 1
1 0 1
1 1 0

Example:

                            #include <stdio.h>

int main(){
    int a=3,b=5,c;
    c = a ^ b;
    printf("%d",c);
    return 0;
}
                            
                        

Output:

                            
1
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ $$ (5)_{10} = (101)_2 $$ $$ (11)_2 \wedge (101)_2 = (001)_2 = (1)_{10} $$

Bitwise NOT

Operator : ~

Inverts all bits of it

Truth Table

A ~
0 1
1 0

Example:

                            #include <stdio.h>

int main(){
    int a=3,b;
    b = ~a;
    printf("%d",b);
    return 0;
}
                            
                        

Output:

                            
-4
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ The result will be in 2's complement form $ ~N = -(N+1) $ $$ ~(3)_{10} = (-4)_{10} $$

Left Shift

Operator : <<

Left shifts the bits of the first operand, the second operand decides the number of places to shift.

Example:

                            #include <stdio.h>

int main(){
    int a=3,b=2,c;
    c = a << b;
    printf("%d",c);
    return 0;
}
                            
                        

Output:

                            
12
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ $$ Left shift by 2 $$ $$ (1100)_2 = (12)_{10} $$

Right Shift

Operator : >>

Right shifts the bits of the first operand, the second operand decides the number of places to shift.

Example:

                            #include <stdio.h>

int main(){
    int a=3,b=1,c;
    c = a >> b;
    printf("%d",c);
    return 0;
}
                            
                        

Output:

                            
1
                            
                        

Example:

$$ (3)_{10} = (11)_2 $$ $$ Right shift by 1 $$ $$ (01)_2 = (1)_{10} $$