## Jun 14, 2014

### C Programming #10: Operators - Bitwise

Following section will discuss the various bit manipulation operations provided by C. Note that  they work only on integer  datatype(char, int, long) variable/constant. It is always better to work with unsigned integer as these operation on signed bit though gives value but does not make sense.

Following are the Bit-wise operators
1. Bit-wise AND
2. Bit-wise OR
3. Bit-wise XOR
4. Bit-wise NOT (One's complement)
5. Bit-wise left shift
6. Bit-wise right shift
Already some of the terms such as AND, OR, NOT are already covered in Logical operators. The difference between the logical operators and bit-wise operators is that, logical operators take the whole value to be true or false and get the result, while the bit-wise operator does the same operations on bit level. So now we know the differences let us dive into each one of the operator in deeply.

## Bit-wise AND

'&' is the bit-wise AND operator in C. The various combination of the o/p is discussed below

 A B A and B 0 0 0 0 1 0 1 0 0 1 1 1

Above truth table tells how it works on 1 bit, say we want to find bit-wise AND of 25 & 35. First we need to find binary representation of each, 25 = 0001 1001 and 35 = 0010 0011. Write down both the representation one below another and work on finding the corresponding bit AND.

0001 1001
0010 0011
& ---------
0000 0001

The result is 0000 0001 in binary which is 1 in decimal.

Say we had done Logical AND between 25 and 35 then; 25 = non-zero(true), 35 means non-zero(true). hence Logical AND between them is true, which means 1. In this particular case the result is same for logical and bit-wise AND. Take another example of 25 and 34. Logical AND - 1; Bit-wise AND - 0

Hence be very careful.

Same can be implemented as C Program as follows
(Note in the following example data type unsigned char is used to store integer, rather than ASCII value !!)

#include <stdio.h>
int main()
{
unsigned char a = 25;
unsigned char b = 35;

printf("Bitwise AND between a and b is %d\n", a & b);
printf("Logical AND between a and b is %d\n", a && b );
printf("Bitwise AND between 25 and 34 is %d\n", 25 & 34);
printf("Logical AND between 25 and 34 is %d\n", 25 && 34 );

return 0;
}


output of the above program is

Bitwise AND between a and b is 1
Logical AND between a and b is 1
Bitwise AND between 25 and 34 is 0
Logical AND between 25 and 34 is 1


One of primary use of AND operator is to make particular bit/bits position as 0 (called as resetting a bit). Say i want to reset BIT position 2 of number 19 (0001 0011 in binary). Then we need to create a mask first; that has all bits 1 except bit 2, which is 1111 1101 in binary. Which is FD in hex which is represented as 0xFD. (In C).

Doing the Bitwise AND
0001 0011
&  1111 1101
------------
0001 0001

Result is 0001 0001(binary) which is 17 in decimal. We can also reset multiple bits too.

#include <stdio.h>
int main()
{
unsigned char a = 19;

printf("Second Bit reset of a %d\n", a & 0xFD);
printf("First Bit and Second Bit reset of a %d\n", a & 0xFC);

return 0;
}


Output of the above program is

Second Bit reset of a 17
First Bit and Second Bit reset of a 16


## Bitwise OR

'|' is the bitwise OR operator in C. The various combination of the o/p is discussed below

 A B A or B 0 0 0 0 1 1 1 0 1 1 1 1

Above Truth table tells how it works on 1 bit, say we want to find Bitwise OR of 25 & 35.

0001 1001
0010 0011
| ---------
0011 1011

The result is 0011 1011 in binary which is 59 in decimal. While the Logical OR between these two value is 1.

Following C program that does the same.

#include <stdio.h>
int main()
{
unsigned char a = 25;
unsigned char b = 35;

printf("Bitwise OR between a and b is %d\n", a | b);
printf("Logical OR between a and b is %d\n", a || b );

return 0;
}


output of the above program is

Bitwise OR between a and b is 59
Logical OR between a and b is 1

One of primary use of OR operator is to make particular bit/bits position as 1 (called as setting a bit). Say i want to set BIT position 3 of number 19 (0001 0011 in binary). Then we need to create a mask first; that has all bits 0 except bit 3, which is 0000 0100 in binary. Which is 04 in hex which is represented as 0x04. (In C).

Doing the Bitwise OR
0001 0011
&  0000 0100
------------
0001 0111

Result is 0001 0111(binary) which is 23 in decimal. We can also reset multiple bits too.

#include <stdio.h>
int main()
{
unsigned char a = 19;

printf("Second Bit set of a %d\n", a | 0x04);
printf("Third Bit and Fourth Bit set of a %d\n", a | 0x0C);

return 0;
}


Output of the above program is

Second Bit set of a 23
Third Bit and Fourth Bit set of a 31


## Bitwise XOR

'^' is the bitwise XOR operator in C. The various combination of the o/p is discussed below

 A B A xor B 0 0 0 0 1 1 1 0 1 1 1 0
Above Truth table tells how it works on 1 bit, say we want to find Bitwise XOR of 25 & 35.

0001 1001
0010 0011
^ ---------
0011 1010

The result is 0011 1010 in binary which is 58 in decimal. Note that there is no Logical XOR.

Following C program that does the same.

#include <stdio.h>
int main()
{
unsigned char a = 25;
unsigned char b = 35;

printf("Bit-wise XOR between a and b is %d\n", a ^ b);

return 0;
}

output of the above program is

Bit-wise XOR between a and b is 58

One of primary use of XOR operator is to make particular bit/bits position toggle. (Means if there was 1 it should be made 0 and if there was 0 then it should be made 0). Say i want to toggle BIT position 3 and 2 of number 19 (0001 0011 in binary). Then we need to create a mask first; that has all bits 0 except bit 2 and 3, which is 0000 0110 in binary. Which is 06 in hex which is represented as 0x06. (In C).

Doing the Bit-wise XOR
0001 0011
^  0000 0110
------------
0001 0101

Result is 0001 0101(binary) which is 21 in decimal.

#include <stdio.h>
int main()
{
unsigned char a = 19;

printf("Toggle bit 2 and 3 of a is %d\n", a ^ 0x06);

return 0;
}

Output of the above program is

Toggle bit 2 and 3 of a is 21


## Bit-wise NOT

'~' is the bit-wise NOT operator in C. The various combination of the o/p is discussed below

 A NOT A 0 1 1 0
Above Truth table tells how it works on 1 bit, say we want to find NOT of 25.

~  0001 1001
------------
1110 0110

The result is 1110 0110 in binary which is 230 in decimal. This is very different from Logical NOT. Logical NOT of 25 would be 0.

Following C program that does the same.

#include <stdio.h>
int main()
{
unsigned char a = 25;

printf("Bitwise NOT of a is %d\n", (unsigned char)~a);
printf("Logical NOT of a is %d\n", !a);

return 0;
}


output of the above program is

Bitwise NOT of a is 230
Logical NOT of a is 0

unsigned char infront of ~a is called type casting, which will covered later. Just remember that without it the answer will be wrong.

## Bit-wise left shift

'<<' is the bit-wise left shift operator in C.

Syntax is as follows
a << b
Then a is shifted by b bits to left

For example
23 << 3
Convert 23 into binary which is 0001 0111. shifting it by 3 bits is 1011 1000 which is 184

Program that depicts left shift

#include <stdio.h>
int main()
{
unsigned char a = 23;

printf("Left shift 23 by 3 is %d\n", a << 3);

return 0;
}

Output of above program is

Left shift 23 by 3 is 184


## Bit-wise right shift

'>>' is the bit-wise right shift operator in C.

Syntax is as follows
a >> b
Then a is shifted by b bits to right.

For example
23 >> 1
Convert 23 into binary which is 0001 0111, shifting it by 1 bits is 0000 1011 towards right which is 11 in decimal.

Program that depicts right shift

#include <stdio.h>
int main()
{
unsigned char a = 23;

printf("Right shift 23 by 1 is %d\n", a >> 1);

return 0;
}

Output of above program is

Right shift 23 by 1 is 11