Skip to main content

Bitwise Operators

Bitwise operators

  • Bitwise operators in C, are used to work with bits and perform bit-by-bit operation.
  • They are equivalent to arithmetic operations like ( +, -, /, * ) the only difference is, They operates on bits instead of numbers.

The following are the bitwise operators in C:

& (bitwise AND)

  • This operator compares each bit of the first operand to the corresponding bit of the second operand.
  • If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

The syntax for the & operator is as follows:

result = operand1 & operand2;

| (bitwise OR)

  • This operator compares each bit of the first operand to the corresponding bit of the second operand.
  • If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

The syntax for the | operator is as follows:

result = operand1 | operand2;

^ (bitwise XOR)

  • This operator compares each bit of the first operand to the corresponding bit of the second operand.
  • If the bits are different, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

The syntax for the ^ operator is as follows:

result = operand1 ^ operand2;

The truth tables for &, |, and ^.

PQP & QP | QP ^ Q
00000
01011
11110
10011

~ (bitwise NOT)

  • This operator inverts all the bits of the operand.

The syntax for the ~ operator is as follows:

result = ~operand;

Bitwise left shift operator (<<)

This operator shifts the bits of the operand to the left by the specified number of positions.

The syntax for the << operator is as follows:

result = operand << shift_count;

Bitwise right shift operator (>>)

This operator shifts the bits of the operand to the right by the specified number of positions.

The syntax for the >> operator is as follows:

result = operand >> shift_count;

Examples of Bitwise operators

Example 1

In this example, we will see the use of bitwise AND, OR, XOR and NOT operators:

#include <stdio.h>

int main()
{
int x = 5; // binary: 00101
int y = 10; // binary: 01010

// using & (bitwise AND) operator
int result = x & y;
printf("%d & %d = %d\n", x, y, result); // prints: 5 & 10 = 0

// using | (bitwise OR) operator
result = x | y;
printf("%d | %d = %d\n", x, y, result); // prints: 5 | 10 = 15

// using ^ (bitwise XOR) operator
result = x ^ y;
printf("%d ^ %d = %d\n", x, y, result); // prints: 5 ^ 10 = 15

// using ~ (bitwise NOT) operator
result = ~x;
printf("~%d = %d\n", x, result); // prints: ~5 = -6

return 0;
}
Output:

Output:

5 & 10 = 0
5 | 10 = 15
5 ^ 10 = 15
~5 = -6

Explanation:

Here, we have defined two variables x and y with values 5 and 10 respectively.

  • In the first statement, we are using the & operator to perform bitwise AND operation on x and y. The result is 0, which is the bitwise AND of the binary representation of x and y.

  • In the second statement, we are using the | operator to perform bitwise OR operation on x and y. The result is 15, which is the bitwise OR of the binary representation of x and y.

  • In the third statement, we are using the ^ operator to perform bitwise XOR operation on x and y. The result is 15, which is the bitwise XOR of the binary representation of x and y.

  • In the fourth statement, we are using the ~ operator to perform bitwise NOT operation on x. The result is -6, which is the bitwise NOT of the binary representation of x.

Example 2

In this example, we will see the use of Left and Right Shift operators:

#include <stdio.h>

int main()
{
int x = 5; // binary: 00101
int y = 6; // binary: 00110

// using << (left shift) operator
printf("%d << 1 = %d\n", x, x << 1); // prints: 10

// using >> (right shift) operator
printf("%d >> 1 = %d\n", y, y >> 1); // prints: 3

return 0;
}
Output:

Output:

5 << 1 = 10
6 >> 1 = 3

Explanation:

Here, we have defined two variables x and y with values 5 and 6 respectively.

  • In the first printf statement, we are using the << operator to perform left Shift operation on x. The result is 10.

  • In the first printf statement, we are using the >> operator to perform right Shift operation on y. The result is 3