**Bitwise operators in C**

Bitwise operators are used to manipulate individual bits of a numeric value. There are six bitwise operators provided by C language.

- Bitwise AND(&)
- Bitwise OR (|)
- Bitwise XOR(^)
- Bitwise complement(~)
- Bitwise Left shift(,)
- Bitwise Right shift(>>)

******Bitwise form of a number is obtained by converting the number into binary number.**

**1. Bitwise AND**

Bitwise AND operator is represented as single ampersand sign (&).

If any of the input to this operator is 0, output would be 0.

If all the inputs of this operator are 1, output would be 1.

Values are converted into bitwise form and then their individual bits are applied bitwise And operation. Depending upon the bit values, a new value will be generated.

**Truth table for Bitwise And operator is**

Input1 | Input2 | Output |

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

Program to demonstrate bitwise AND operator |

#include<stdio.h>int main(){int a=10,b=7,c;c=a&b; printf(“\nc=%d”,c);return(0);} |

Output |

c=2 |

**Description**

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. After conversion following bit values will be formed and bitwise AND will be applied as follows:

**a=10** will be represented in bitwise form as **0000000000001010****
b=7** will be represented in bitwise form as

**0000000000000111**

**a&b will generate 0000000000000010**

After converting 0000000000000010 into decimal form, we get 2, so value of a&b would be 2.

**2. Bitwise OR (|)**

Bitwise OR operator is represented as single pipe sign (|).

If any of the input to this operator is 1, Output would be 1.

If all the inputs of this operator are 0, Output would be 0.

Values are converted into bitwise form and then their individual bits are applied bitwise OR operation. Depending upon the bit values, a new value will be generated.

**Truth table for Bitwise OR operator is**

Input1 | Input2 | Output |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

Program to demonstrate bitwise OR operator |

#include<stdio.h>int main(){int a=10,b=7,c;c=a|b;printf(“\nc=%d”,c);return(0);} |

Output |

c=15 |

**Description**

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. After conversion following bit values will be formed and bitwise OR will be applied as follows:

**a=10 ** will be represented in bitwise form as **0000000000001010**

**b=7 ** will be represented in bitwise form as **0000000000000111**

**a|b** will generate **0000000000001111**

After converting 0000000000001111 into decimal form, we get 15, so value of a|b would be 15.

**3. Bitwise XOR (^)**

Bitwise XOR operator is represented as caret sign (^).

If odd number of inputs to this operator is 1, Output would be 1.

Values are converted into bitwise form and then their individual bits are applied bitwise XOR operation. Depending upon the bitwise values, a new value will be generated.

**Truth table for Bitwise XOR operator is**

Input1 | Input2 | Output |

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

** **

Program to demonstrate bitwise XOR operator |

#include<stdio.h>int main(){int a=10,b=7,c;c=a^b;printf(“\nc=%d”,c);return(0);} |

Output |

c=13 |

**Description**

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. XOR will be applied as follows:

**a=10** will be represented in bitwise form as **0000000000001010**

**b=7** will be represented in bitwise form as **0000000000000111**

**a^b** will generate **0000000000001101**

After converting 0000000000001101 into decimal form, we get 13, so value of a^b would be 13.

**4. Bitwise complement (~) **

Bitwise complement operator is also known as bitwise NOT operator. It is represented as tild sign (~). It is used to reverse the input.

If input of this operator is 1, Output would be 0.

If input of this operator is 0, Output would be 1.

Value is converted into bitwise form and then individual bits are applied bitwise complement operation.

After applying this operation, all individual bits are reversed i.e. all 0’s become 1’s and all 1’s become 0’s.

Depending upon the bitwise values, a new value will be generated.

**Truth table for Bitwise Complement operator is**

Input | Output |

0 | 1 |

1 | 0 |

Program to demonstrate bitwise complement operator |

#include<stdio.h>int main(){unsigned int a=65535,b;b=~a;printf(“\nb=%u”,b);return(0);} |

Output |

b=0 |

### Description

** **In the above program, variable a contains 65535.

a=65535 will be represented in bitwise form as **1111111111111111**

~a will generate **0000000000000000**

After converting **0000000000000000** into decimal form, we get 0, so value of b would be 0.

**5. Bitwise Left Shift(<<) **

Bitwise Left Shift operator is represented as double less than sign(<<).

It is used to shift individual bits of a numeric value towards left by specified number of digits.** **

Program to demonstrate Left Shift operator |

#include<stdio.h>int main(){int a=10,b;b=(a<<1);printf(“\nb=%d”,b);return(0);} |

Output |

b=20 |

### Description

In the above program, variable a contains 10. Bitwise left shift operator will shift position of each bit towards left by one place as follows:

a=10 is represented in bitwise form as **0000000000001010**

a<<1 will generate **0000000000010100**

After converting **0000000000010100** into decimal form, we get 20, so value of b would be 20.

**6. Bitwise Right Shift (>>)**

Bitwise Right Shift operator is represented as double greater than sign(>>). It is used to shift individual bits of a numeric value towards right by specified number of digits.

Program to demonstrate the use of right Shift operator |

#include<stdio.h>int main(){int a=10,b;b=(a>>1);printf(“\nb=%d”,b);return(0);} |

Output |

b=5 |

### Description

In the above program, variable a contains 10. Bitwise Right shift operator will shift position of each bit towards right by one place.

a=10 will be represented in bitwise form as **0000000000001010**

a>>1 will generate **0000000000000101**

After converting **0000000000000101** into decimal form, we get 5, so value of b would be 5.

**Best Books of C**

**Tips and Tricks**

- Bitwise operators can be used for encrypting or decrypting the data.
- Bitwise AND operator is suitable for checking whether a particular bit is on or off.
- Bitwise AND operator is suitable for turning off a particular bit in a number.
- Bitwise OR operator is suitable for turning on a particular bit in a number.