Bitwise Operators in C programming

Bitwise operators are special types of operators that are used in programming the processor. In processor, mathematical operations like: addition, subtraction, addition and division are done using the bitwise operators which makes processing faster and saves power.

OperatorsMeaning of operators
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
~Bitwise complement
<<Shift left
>>Shift right

Bitwise AND operator in C programming.

The output of logical AND is 1 if both the corresponding bits of operand is 1. If either of bit is 0 or both bits are 0, the output will be 0. It is a binary operator(works on two operands) and indicated in C programming by & symbol. Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
  00001100
& 00011001
  ________
  00001000  = 8 (In decimal)

As, every bitwise operator works on each bit of data. The corresponding bits of two inputs are check and if both bits are 1 then only the output will be 1. In this case, both bits are 1 at only one position,i.e, fourth position from the right, hence the output bit of that position is 1 and all other bits are 0.


#include <stdio.h>
int main()
{
    int a=12,b=39;
    printf("Output=%d",a&b);
    return 0;
}

Output

Output=4

 

Bitwise OR operator in C

The output of bitwise OR is 1 if either of the bit is 1 or both the bits are 1. In C Programming, bitwise OR operator is denoted by |.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25
  00001100
| 00011001
  ________
  00011101  = 29 (In decimal)

#include <stdio.h>
int main()
{
    int a=12,b=25;
    printf("Output=%d",a&b);
    return 0;
}

Output

Output=29

C Programming Bitwise XOR(exclusive OR) operator

The output of bitwise XOR operator is 1 if the corresponding bits of two operators are opposite(i.e., To get corresponding output bit 1; if corresponding bit of first operand is 0 then, corresponding bit of second operand should be 1 and vice-versa.). It is denoted by ^.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25
  00001100
| 00011001
  ________
  00010101  = 21 (In decimal)

#include <stdio.h>
int main()
{
    int a=12,b=25;
    printf("Output=%d",a^b);
    return 0;
}
Output=21

Bitwise compliment operator

Bitwise compliment operator is an unary operator(works on one operand only). It changes the corresponding bit of the operand to opposite bit,i.e., 0 to 1 and 1 to 0. It is denoted by ~.

35=00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011 
  ________
  11011100  = 220 (In decimal)

Twist in bitwise complement operator in C Programming

Output of ~35 shown by compiler won't be 220, instead it shows -36. For any integer n, bitwise complement of n will be -(n+1). To understand this, you should understand the concept of 2's complement.

2's Complement

Two's complement is the operation on binary numbers which allows number to write it in different form. The 2's complement of number is equal to the complement of number plus 1. For example:

 Decimal         Binary           2's complement 
   0            00000000           -(11111111+1) = -00000000 = -0(decimal)
   1            00000001           -(11111110+1) = -11111111 = -256(decimal)
   12           00001100           -(11110011+1) = -11110100 = -244(decimal)
   220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

If we consider the bitwise complement of 35, 220(in decimal) is converted into 2's complement which is -36. Thus, the output shown by computer will be -36 instead of 220.

How is bitwise complement of any number N=-(N+1)?

bitwise complement of N= ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)
#include <stdio.h>
int main()
{
    printf("complement=%d\n",~35);
    printf("complement=%d\n",~-12);
    return 0;
}
complement=-36
Output=11

Shift Operator in C programming

There are two shift operators in C programming: Right shift operator and Left shift operator.

Right Shift Operator

Right shift operator moves the all bits towards the right by certain number of bits which can be specified. It is denoted by >>.

212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000 
212>>0 = 11010100 (No Shift)

Left Shift Operator

Left shift operator moves the all bits towards the left by certain number of bits which can be specified. It is denoted by <<.

212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

#include <stdio.h>
int main()
{
    int num=212,i;
    for (i=0;i<=2;++i)
        printf("Right shift by %d: %d\n",i,num>>i);
     printf("\n");
     for (i=0;i<=2;++i) 
        printf("Left shift by %d: %d\n",i,num<<i);    
        return 0;
}

Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53

Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848

Interesting thing to note in Left and Right Shift

For any positive number, right shift is equal to integer division of that number by (shift bit plus one) and for any integer left shift is equal to the multiplication of that number by (shift bit plus one).