Bitwise Operators in C

We know that computers operate on one byte at a time i.e. 8 bits of data a manipulated as one unit. However, these are situations wherein we may need to know and operate on individual bits, like say depending on whether the 5th bit is 1 or 0, some operations have to be done. Such situations occur in several applications, like in systems programming. ‘C’ provides facilities for such operations.

The normally used bit operators are listed below

Symbol Operation
& Bitwise AND
^ Bitwise Exclusive-OR
~ Ones Complement
» Right Shift
<< Left Shift

For those not familiar with boolean algebra, a brief description of the above operations follows. Bitwise AND: this operation on 2 bits. When both bits are 1, the output is one, otherwise, it is Zero. Consider the example of bitwise ANDing of 14 and 12.

In a 8 bit machine, it turns out to be

Result 00001100

Bitwise inclusive OR: This also operates on two bits. The output is 1 if either of the two bits is 1, otherwise it is 0. Consider

 The result is    11111011

Bitwise Exclusive OR: This operation returns a 1 if either of the two bits (but not both) is a 1. The ones complement : This works on a single number and simply converts each 1 to 0 and each 0 to 1. Consider 01101101 After complementing it becomes 10010010.

The left shift operator: this also works on a single number. Each bit of the number is shifted one bit to its left. The leftmost bit is lost and the rightmost bit, that becomes vacant is filled with a 0.

Consider 00101000 = 40 After Shifting left 01010000 = 80.

Note also that a left shift operation results in the number getting multiplied by 2.(There is an exception to this. Figure out when it is so).

The right shift operator - This is the reuses of the left shift operator. It just shifts each bit one place to its right. The rightmost bit is lost, the leftmost bit is made 0.

00101000 = 40 becomes 00010100 = 20

Note that this results in the number getting divided by 2. Though the concepts presented are simple, we write one program that uses the concept. Write a program to rotate a given number called value a given number of times, n. If n is positive rotate it left, otherwise right.

Note: Rotation means shifting each bit by one place and recovering the lost bit. For example, In left shift, each bit is shifted one place to the left and the leftmost bit, which comes out is returned to the rightmost place.

Function to rotate an unsigned int left or right

unsigned int rotate (value, n)
unsigned int value;
int n;
unsigned int result, bits;
if(n== 0|| n== -16 || n== 16)
return (value)l
else if (n > 0) /* left rotate */
bits = value << (16 - n);
result = value << n | bits;
 n= -n;
bits = value <> n | bits;
result = value >> n | bits;
return (result);
 unsigned int w1 = oxalb5, w2 = Oxff22;
 printf("%x\n", rotate (w1, 4);
 printf("%x\n", rotate (w1, -4);
 printf("%x\n", rotate (w2, 8);
 printf("%x\n", rotate (w2, -2);
 printf("%x\n", rotate (w1, 0);

Output of the above program;