Bitwise operators are used to perform operations directly on the individual bits of integer values. They are useful in low-level programming, embedded systems, device programming, flags, masks, and other situations where individual bits need to be manipulated.
&, |, ^, ~,
<<, and >>.
Bitwise operators work on the binary representation of integer operands. Instead of treating a number only as a complete value, they operate on individual bits.
unsigned int a = 5;
unsigned int b = 3;
printf("%u", a & b);
Here, the & operator performs a bitwise AND operation.
Computers represent integer values using binary digits called bits.
Each bit can contain either 0 or 1.
Decimal 5 = Binary 0101
Decimal 3 = Binary 0011
Bitwise operators work with these binary bits.
| Operator | Name |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left Shift |
>> |
Right Shift |
The bitwise AND operator compares corresponding bits. A result bit is
1 only when both corresponding bits are 1.
5 = 0101
3 = 0011
----
0001
Therefore:
5 & 3 = 1
#include <stdio.h>
int main()
{
unsigned int a = 5;
unsigned int b = 3;
printf("%u", a & b);
return 0;
}
Output:
1
| Bit A | Bit B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Only 1 & 1 produces 1.
The bitwise OR operator produces a 1 when at least one of the
corresponding bits is 1.
5 = 0101
3 = 0011
----
0111
Therefore:
5 | 3 = 7
#include <stdio.h>
int main()
{
unsigned int a = 5;
unsigned int b = 3;
printf("%u", a | b);
return 0;
}
Output:
7
| Bit A | Bit B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Only 0 | 0 produces 0.
The bitwise XOR operator produces 1 when the two corresponding
bits are different.
5 = 0101
3 = 0011
----
0110
Therefore:
5 ^ 3 = 6
#include <stdio.h>
int main()
{
unsigned int a = 5;
unsigned int b = 3;
printf("%u", a ^ b);
return 0;
}
Output:
6
| Bit A | Bit B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
XOR produces 1 when the bits are different.
The bitwise NOT operator ~ reverses every bit of its operand.
Each 0 becomes 1, and each 1 becomes
0.
Binary:
00000101
After ~:
11111010
The exact decimal result depends on the type and representation of the operand.
#include <stdio.h>
int main()
{
unsigned int value = 5;
printf("%u", ~value);
return 0;
}
For an unsigned integer, the result is the bitwise complement of the value across the width of the type.
The left shift operator moves the bits of its left operand toward the left by the number of positions specified by the right operand.
unsigned int number = 5;
number << 1
For this positive value, the binary representation is shifted one position
to the left, producing 10.
5 = 0101
5 << 1 = 1010
Result = 10
#include <stdio.h>
int main()
{
unsigned int number = 5;
printf("%u", number << 1);
return 0;
}
Output:
10
The right shift operator moves the bits of its left operand toward the right by the number of positions specified by the right operand.
unsigned int number = 8;
number >> 1
For this unsigned value:
8 = 1000
8 >> 1 = 0100
Result = 4
#include <stdio.h>
int main()
{
unsigned int number = 8;
printf("%u", number >> 1);
return 0;
}
Output:
4
Bitwise AND is often used with a mask to check whether a particular bit is set.
unsigned int value = 5;
if (value & 1)
{
printf("Last bit is 1");
}
The expression checks the least significant bit of the value.
For non-negative integers, the least significant bit can be used to check whether a number is even or odd.
unsigned int number = 7;
if (number & 1)
{
printf("Odd");
}
else
{
printf("Even");
}
Output:
Odd
The &= operator performs bitwise AND and assigns the result
back to the left operand.
unsigned int a = 7;
unsigned int b = 3;
a &= b;
printf("%u", a);
Output:
3
It is equivalent to a = a & b;.
unsigned int a = 4;
unsigned int b = 2;
a |= b;
printf("%u", a);
Output:
6
It is equivalent to a = a | b;.
unsigned int a = 7;
unsigned int b = 3;
a ^= b;
printf("%u", a);
Output:
4
It is equivalent to a = a ^ b;.
unsigned int number = 4;
number <<= 1;
printf("%u", number);
Output:
8
The statement is equivalent to number = number << 1;.
unsigned int number = 8;
number >>= 1;
printf("%u", number);
Output:
4
The statement is equivalent to number = number >> 1;.
A bit mask is a value used with bitwise operators to select or manipulate specific bits.
unsigned int value = 13;
unsigned int mask = 4;
if (value & mask)
{
printf("Bit is set");
}
The mask selects the bit represented by the value 4.
& with logical AND
&&.| with logical OR ||.#include <stdio.h>
int main()
{
unsigned int a = 5;
unsigned int b = 3;
printf("AND = %u\n", a & b);
printf("OR = %u\n", a | b);
printf("XOR = %u\n", a ^ b);
printf("Left Shift = %u\n", a << 1);
printf("Right Shift = %u\n", a >> 1);
return 0;
}
Output:
AND = 1
OR = 7
XOR = 6
Left Shift = 10
Right Shift = 2
#include <stdio.h>
int main()
{
unsigned int a, b;
printf("Enter two positive integers: ");
scanf("%u %u", &a, &b);
printf("AND = %u\n", a & b);
printf("OR = %u\n", a | b);
printf("XOR = %u\n", a ^ b);
return 0;
}
This program accepts two unsigned integer values and performs three common bitwise operations.
Practice the following programs:
& 1.&=, |=, and ^=.Practice binary representations along with these operators to understand how individual bits are manipulated.
& performs bitwise AND.| performs bitwise OR.^ performs bitwise XOR.~ performs bitwise NOT.<< shifts bits to the left.>> shifts bits to the right.Question: Which operator performs bitwise AND in C?