Bitwise Operators in C/C++

Bitwise operators in C or C++ work on bit level so they are called bitwise. There are 6 bitwise operators in C programming:
1. & (Bitwise AND)
2. | (Bitwise OR)
3. ~ (Bitwise NOT)
4. ^ (Bitwise XOR)
5. << (Left Shift)
6. >> (Right Shift)
Example program to demonstrate use of bitwise operators in C :
#include<stdio.h>

int main()
{

unsigned char x = 12, y = 19;

printf("\n x = %d, %, y = %d ", x, y);

printf("\n x & y = %d ", x&y);

printf("\n x | y = %d ", x|y);

printf("\n x ^ y = %d ", x^y);

printf("\n ~x = %d ", x = ~x);

printf("\n y << 1 = %d ", y<<1);

printf("\n y >> 1 = %d", y>>1);

return 0;
}

/* Output of above code:-

x = 12, y = 19 // x = 00001100, y = 00010011
x & y = 0
x | y = 31 // 31 result in bits 00011111
x ^ y = 31 // 31 result in bits 00011111
~x = 243 // 243 result in bits 11110011
y << 1 = 38 // 38 result in bits 00100110
y >> 1 = 9 // 9 result in bits 00001001

*/

Bitwise AND (&)

Bitwise AND (&) is a binary operator. It means that it requires two numbers as operands. It performs AND operation on every bit of two numbers. The result of AND is 1 only if both bits are 1.

Example

a = 1000

b = 1101

Bitwise AND : a & b = 1000

Bitwise OR (|)

Bitwise OR (|) is also a binary operator which means it needs two numbers as operands. It performs OR operation on every corresponding bits of two numbers. The result of bitwise OR is 1 if any of the two bits is 1.

Example

a = 1000

b = 1101

Bitwise OR : a | b = 1101

Bitwise XOR (^)

Bitwise XOR is also a binary operator which need two numbers as operands. It performs XOR on every corresponding two bits of two numbers. The result of XOR is 1 if the two bits are different. In other words, XOR gives 0 if both the bits are same.

Example

a = 1000

b = 1101

Bitwise XOR : a ^ b = 0101

Left Shift (<<) Operator

Left shift (<<) is also a binary operator which means it operates on two operands. The first operand is a number which is to be left shifted and the second operand is also a number which decides number of bits to shift to left.

Example

number = 101001

shiftBy = 2

Bitwise Left Shift : number << shiftBy = 100100
The trailing places to the right are filled with zeros. Shifting a bit to left is equivalent to multiplying the integer by 2.

Right Shift (>>) Operator

Right shift (>>) is also a binary operator. It is just like left shift but instead of shifting the bits towards left it shifts them to right. The first operand is number to be shifted and second operand is also a number which decides number of bits to shift to right.

Example

number = 101001

shiftBy = 2

Bitwise Right Shift : number >> shiftBy = 001010
The leading places to the left are filled with zeros. Shifting a bit to the right is equivalent to division by 2.

Note : Remember that if any one of the operands for left shit (<<) or right shift (>>) is negative the result is undefined behaviour. Also if the number is shifted more than it's size in bits, undefined behaviour occurs. For example, 2 << 33 will result in undefined behaviour if the integer 2 is stored as a 32 bit number.

Bitwise NOT (~)

Bitwise NOT (~) is a unary operator meaning that it only takes one input number and inverts all its bits.

Example

number = 101001

Bitwise NOT : ~ number = 010110