Tuesday, September 6, 2011

Bitwise operators

The smallest element in memory on which are able to operate is called byte; and the programming language are byte oriented. One of the c powerful features is a set of bit manipulation operators. There are various bitwise operators in C as following table; we learn all step by step :
Operators Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR(exclusive OR)
>> Left shift
<< right shift
~ One's complement


These operators can operate upon ints and chars but not on floats and doubles. Bits are numbered from zero onwards, increasing from right to left as following figure:

7 6 5 4 3 2 1 0
8 bit Character


16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
16 bit Integer


Bitwise AND Operator (&)

The & operator operates on a pair of bits to yield a resultant bit. The rules that decide the value of the resultant bit also called truth table are shown below:

Bit x Bit y x & y
0 0 0
0 1 0
1 0 0
1 1 1

The best use of the AND operator is to check whether a particular bit of an operand is ON or OFF. The meaning of ON means 1 and OFF means 0. The following program puts this logic clear:


 /*Example of bitwise AND operator*/

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int x=45;
  int y=37;
  int z;
  clrscr();
  z = x & y;
  printf("\nValue of z=%d",z);
 }




 Output of above program:

 Value of z=37


In above program compiler first calculate the binary x and y which is 111101 and 100101 respectly. After this its work on bitwise operator and final result is 37 which binary code are 100101. The following table shown the result : 
Values binary code
x = 45 111101
y = 37 100101
z = x & y 100101


Bitwise OR ( | )

Truth table for bitwise OR are as following :
x y z = x | y
0 0 0
0 1 1
1 0 1
1 1 1

Bitwise XOR ( ^ )

It is also called exclusive OR. If there are similar bit then its return 0 otherwise its return 1. Let watch truth table of  x^y :
x y z = x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

Example of exclusive OR :
x = 75
y = 64
z = x ^ y

 x = 1 0 0 1 0 1 1
 y = 1 0 0 0 0 0 0
 z = 0 0 0 1 0 1 1
 
hence, the value of z=11

Bitwise right shift ( >> )

Right shift operator shifts each bit in its left operand to the right. The number of places the bits are shifted depends on the number following the operator i.e. its right operand.
In simple meaning of bitwise right shift as :

            insertion(zero)  >  deletation

Example of right shift operator :
x = 15
y = x >> 2

[x=15 before right shifting ]         0000  0000  0000  1111
[y = x >> 2 during right shifting]  000000  0000  0000  1111
[y = x >> 2 after right shifting]     0000  0000  0000  0011

so now value of y=3.

Left Shift operator ( << )

This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each shifted, a 0 is added to the right of the number. Following structure shows it :

  deletatio   >  insertion(zero)  

Example of right shift operator :
x = 15
y = x << 2

[x=15 before right shifting ]            0000  0000  0000  1111
[y = x << 2 during right shifting]     0000  0000  0000  111100
[y = x << 2 after right shifting]        0000  0000  0011  1100


so now value of y=60.


One's complement ( ~ ) or Tilde

It is reverse the bit i.e. if bit is 0 then its return 1 and when bit is 1 it is return 0.
example of tilde:
x = 15
y = ~ x

(15)10 = 0000 0000 0000 1111
y = ~ x   1111 1111 1111 0000
hense, 65535 - 15 = 65520.
so the value of y is 65520. 

No comments:

Post a Comment