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 ^
.
P | Q | P & Q | P | Q | P ^ Q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
~ (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 performbitwise AND
operation onx
andy
. The result is0
, which is the bitwise AND of the binary representation ofx
andy
.In the second statement, we are using the
|
operator to performbitwise OR
operation onx
andy
. The result is15
, which is the bitwise OR of the binary representation ofx
andy
.In the third statement, we are using the
^
operator to performbitwise XOR
operation onx
andy
. The result is15
, which is the bitwise XOR of the binary representation ofx
andy
.In the fourth statement, we are using the
~
operator to performbitwise NOT
operation onx
. The result is-6
, which is the bitwise NOT of the binary representation ofx
.
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 performleft Shift
operation onx
. The result is10
.In the first printf statement, we are using the
>>
operator to performright Shift
operation ony
. The result is3