# 6.12 – Bitwise operators – AND, OR, XOR bitwise operators

by on September 30, 2017

## AND bitwise operator &

It is a binary bitwise operator operates on two operands. Basically it works on bits rather bytes, performs logical AND on the bits in each position of data in its binary format.
Here the two operands must be of same type that is either char or int, not of any other types.
The truth table of AND bitwise operator on bits is

While performing anding, gives 1 if both the bits are 1s and gives 0 if any one or both the bits are 0

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

Output:
17

Here logical AND operation is performed on associated bits.

### Where do we use & bitwise operator

It is used on two purposes

1. To find whether a bit is on or off at a particular position
2. To off the bit at a position

### Bitwise & application 1

Let us discourse the first application of & bitwise operator through an example. If we want to find whether 3rd least significant bit in a binary number is on/off then we AND a mask operand 000000100 (4) to the number.
If the result is 000000100 (4) then the 3rd least significant bit is on, otherwise it is off

```#include <stdio.h>
int main()
{
char ch=25,x;
x=ch&4;
if(x==4)
printf("ON");
else
printf("OFF");
return 0;
}
```

Output
OFF

Here in this example the result is zero, hence 3rd least significant bit is off

```#include <stdio.h>
int main()
{
char ch=21,x;
x=ch&4;
if(x==4)
printf("ON");
else
printf("OFF");
return 0;
}
```

Output
ON

Here in this example the result is 4, hence 3rd least significant bit is on

### Bitwise & application 2

Let us discourse the second application of & bitwise operator through an example. If we want to off the 3rd least significant bit in a binary number then we AND a mask operand 11111011 (~4) to the number. So that only the 3rd least significant bit would be off because OFF AND any thing is OFF and remaining bits would be unchanged

```#include <stdio.h>
int main()
{
char ch=21,x;
x=ch&(~4);
printf("%d",x);
return 0;
}
```

Output
17

Here in this example, as the 3rd least significant bit is 1 and anding with mask operand 11111011 (~4) results 00010001 (17). Where the 3rd least significant bit is OFF while remaining bits remains same.

## OR bitwise operator |

It is another binary bitwise operator operates on two operands. Basically it works on bits rather bytes, performs logical OR on the bits in each position of data in its binary format.
Here the two operands must be of same type that is either char or int, not of any other types.
The truth table of OR bitwise operator on bits is

While performing oring, gives 0 if both the bits are 0s and gives 1 if any one or both the bits are 1

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

Output:
29

Here logical OR operation is performed on associated bits.

### Where do we use | bitwise operator

We generally use the | bitwise operator to ON a bit at a position. Let us discourse the application of | bitwise operator through an example. If we want to ON the 4th least significant bit in a binary number then we OR a mask operand 0000 01000 (8) to the number. So that only the 4th least significant bit would be ON because ON OR any thing is ON and remaining bits would be unchanged

```#include <stdio.h>
int main()
{
char ch=21,x;
x=ch|8;
printf("%d",x);
return 0;
}
```

Output
29

Here in this example, as the 4th least significant bit is 0 and anding with mask operand 0000 1000 results 0001 1101 (29). Where the 4th least significant bit is ON while remaining bits remains same.

## Bitwise XOR operator ^

It is another binary bitwise operator operates on two operands. Basically it works on bits rather bytes, performs exclusive OR on the bits in each position of data in its binary format.
Here the two operands must be of same type that is either char or int, not of any other types.
The truth table of XOR bitwise operator on bits is

While performing xoring, gives 0 if both the bits are 0s or 1s and gives 1 if any one is 0 or 1

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

Output:
12

Here logical XOR operation is performed on associated bits.
Where do we use XOR bitwise operator?
We generally use the ^ bitwise operator to ON/OFF (toggle) a bit at a position. Let us discourse the application of ^ bitwise operator through an example. If we want to ON/OFF the 4th least significant bit in a binary number then we XOR a mask operand 0000 01000 (8) to the number. So that only the 4th least significant bit would be ON if it is OFF, OFF if its is ON
#include<stdio.h>
int main()
{
char ch,dh,x,y;
ch=21;
dh=25;
x=ch^8;
y=dh^8;
printf("%d",x);
printf("\n%d",y);
return 0;
}

Output
29
17

Here in the ch the fourth bit was off, by xoring with a mask operand 0000 1000 (8) we could on the 4th least significant bit

Here in the dh the fourth bit was on, by xoring with a mask operand 0000 1000 (8) we could off the 4th least significant bit
```

Previous post:

Next post: