Bitwise operators are used to perform operations directly on the individual bits of integral values. They are commonly used in low-level programming, flags, masks, embedded systems, and performance-sensitive code.
Bitwise operators work with the binary representation of integer values. A number is represented internally using bits, where each bit can have a value of 0 or 1.
int a = 5;
int b = 3;
In binary, these values can be represented as:
5 = 0101
3 = 0011
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
The & operator performs a bitwise AND operation. A bit in the result is 1 only when both corresponding bits are 1.
int a = 5;
int b = 3;
int result = a & b;
Binary representation:
0101
& 0011
------
0001
Therefore, the result is 1.
| Bit A | Bit B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Only the combination 1 & 1 produces 1.
The | operator performs a bitwise OR operation. A result bit is 1 when at least one of the corresponding bits is 1.
int a = 5;
int b = 3;
int result = a | b;
Binary representation:
0101
| 0011
------
0111
The result is 7.
| Bit A | Bit B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
The ^ operator performs a bitwise exclusive OR operation. A result bit is 1 when the two corresponding bits are different.
int a = 5;
int b = 3;
int result = a ^ b;
Binary representation:
0101
^ 0011
------
0110
The result is 6.
| Bit A | Bit B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
XOR produces 1 when the two bits are different.
The ~ operator is called bitwise NOT. It flips every bit: 0 becomes 1 and 1 becomes 0.
unsigned int a = 5;
unsigned int result = ~a;
The exact numeric result depends on the width of the unsigned type. For example, if a value is considered using 8 bits:
5 = 00000101
~5 = 11111010
Bitwise NOT operates on every bit of the operand.
unsigned char a = 5;
unsigned char result = static_cast<unsigned char>(~a);
Using an unsigned type can make the bit pattern easier to understand when learning bitwise operations.
The << operator shifts the bits of an integer to the left by a specified number of positions.
int number = 5;
int result = number << 1;
For a suitable non-negative value, shifting left by one position corresponds to multiplying by 2.
5 = 0101
5<<1 = 1010
The resulting value is 10 in this example.
int number = 5;
int result = number << 2;
The bits are shifted two positions to the left. For this positive example, the result is 20.
5 = 0101
5<<2 = 10100
The >> operator shifts bits to the right.
unsigned int number = 20;
unsigned int result = number >> 2;
For an unsigned value, shifting right by two positions gives 5.
20 = 10100
20>>2 = 00101
unsigned int number = 16;
unsigned int result = number >> 1;
The result is 8. For an unsigned value, shifting right by one position moves every bit one position toward the right.
int a = 12;
int b = 10;
int andResult = a & b;
int orResult = a | b;
int xorResult = a ^ b;
Each operation works on the individual bits of a and b.
Bitwise AND is often used with a mask to check whether a particular bit is set.
int number = 5;
if (number & 1) {
std::cout << "Odd";
}
For non-negative integers, checking the least significant bit with & 1 can determine whether the number is odd.
The least significant bit of a non-negative integer is 0 for an even number and 1 for an odd number.
int number = 8;
if ((number & 1) == 0) {
std::cout << "Even";
} else {
std::cout << "Odd";
}
A bit mask is a value used to select or test particular bits.
unsigned int number = 10;
unsigned int mask = 4;
if ((number & mask) != 0) {
std::cout << "The selected bit is set";
}
The AND operation keeps only the bits selected by the mask.
Bitwise OR can be used with a mask to set selected bits to 1.
unsigned int number = 8;
unsigned int mask = 2;
number = number | mask;
The bit represented by the mask is set in the resulting value.
A selected bit can be cleared using a combination of AND and NOT.
unsigned int number = 15;
unsigned int mask = 4;
number = number & ~mask;
The bits selected by mask are cleared in the result.
XOR can be used to toggle selected bits. A selected bit changes from 0 to 1 or from 1 to 0.
unsigned int number = 10;
unsigned int mask = 2;
number = number ^ mask;
Only the bit selected by the mask is toggled.
C++ provides compound assignment versions of several bitwise operators.
| Operator | Example | Equivalent |
|---|---|---|
| &= | a &= b |
a = a & b |
| |= | a |= b |
a = a | b |
| ^= | a ^= b |
a = a ^ b |
| <<= | a <<= b |
a = a << b |
| >>= | a >>= b |
a = a >> b |
int number = 12;
number &= 10;
This is equivalent to:
number = number & 10;
The bitwise AND result is stored back in number.
int number = 8;
number |= 2;
This is equivalent to:
number = number | 2;
The selected bits from the right-hand operand are combined with number.
int number = 10;
number ^= 2;
This is equivalent to:
number = number ^ 2;
The selected bits are toggled according to the XOR operation.
#include <iostream>
int main() {
unsigned int a;
unsigned int b;
std::cout << "Enter first number: ";
std::cin >> a;
std::cout << "Enter second number: ";
std::cin >> b;
std::cout << "AND: " << (a & b) << std::endl;
std::cout << "OR: " << (a | b) << std::endl;
std::cout << "XOR: " << (a ^ b) << std::endl;
return 0;
}
C++ can display an integer in binary using std::bitset.
#include <iostream>
#include <bitset>
int main() {
unsigned int number = 5;
std::cout << std::bitset<8>(number);
return 0;
}
The output is:
00000101
#include <iostream>
#include <bitset>
int main() {
unsigned int a = 5;
unsigned int b = 3;
std::cout << "A: "
<< std::bitset<8>(a) << std::endl;
std::cout << "B: "
<< std::bitset<8>(b) << std::endl;
std::cout << "A & B: "
<< std::bitset<8>(a & b) << std::endl;
std::cout << "A | B: "
<< std::bitset<8>(a | b) << std::endl;
std::cout << "A ^ B: "
<< std::bitset<8>(a ^ b) << std::endl;
std::cout << "A << 1: "
<< std::bitset<8>(a << 1) << std::endl;
std::cout << "A >> 1: "
<< std::bitset<8>(a >> 1) << std::endl;
return 0;
}
| Operator | Name | Example | Purpose |
|---|---|---|---|
| & | Bitwise AND | a & b |
Sets a result bit when both bits are 1 |
| | | Bitwise OR | a | b |
Sets a result bit when at least one bit is 1 |
| ^ | Bitwise XOR | a ^ b |
Sets a result bit when the bits are different |
| ~ | Bitwise NOT | ~a |
Flips each bit |
| << | Left Shift | a << 1 |
Shifts bits to the left |
| >> | Right Shift | a >> 1 |
Shifts bits to the right |
Question: What is the result of the following C++ expression?
5 & 3