Bitwise Operators in C++
Bitwise Operators perform operations on the binary (bits) representation of numbers. These operators work directly on the bits of integers.
To understand bitwise operators easily, numbers are first converted into binary form and then the operation is performed on each bit.
Let us take two numbers:
a = 5 → 0101
b = 3 → 0011
List of Bitwise Operators
| Operator | Name | Description | Example |
|---|---|---|---|
| & | Bitwise AND | Returns 1 if both bits are 1 | 5 & 3 → 1 |
| | | Bitwise OR | Returns 1 if at least one bit is 1 | 5 | 3 → 7 |
| ^ | Bitwise XOR | Returns 1 if bits are different | 5 ^ 3 → 6 |
| ~ | Bitwise NOT | Inverts all bits | ~5 → -6 |
| << | Left Shift | Shifts bits to the left | 5 << 1 → 10 |
| >> | Right Shift | Shifts bits to the right | 5 >> 1 → 2 |
Examples of Bitwise Operators
Let us take two numbers:
int a = 5; // binary: 0101
int b = 3; // binary: 0011
1. Bitwise AND (&)
Bitwise AND (&) compares each bit of two numbers and returns
1 only when both bits are 1.
Otherwise the result becomes 0.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 3;
cout << (a & b);
return 0;
}
Output:
1
Explanation:
0101
& 0011
-------
0001 → 1
2. Bitwise OR (|)
Bitwise OR (|) compares each bit of two numbers and returns
1 if at least one bit is 1.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 3;
cout << (a | b);
return 0;
}
Output:
7
Explanation:
0101
| 0011
-------
0111 → 7
3. Bitwise XOR (^)
Bitwise XOR (^) returns 1 when the bits are
different, and returns 0 when the bits are same.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 3;
cout << (a ^ b);
return 0;
}
Output:
6
Explanation:
0101
^ 0011
-------
0110 → 6
4. Bitwise NOT Operator (~)
Bitwise NOT (~) is a unary operator
that reverses all the bits of a number.
It converts every 1 into 0 and every 0 into 1.
This operator works using the concept of 2’s complement, so the result is usually a negative number.
Shortcut Rule (Very Important)
~a = -(a + 1)
Instead of converting into binary every time, you can use this simple formula to find the result quickly.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 5;
cout << ~a;
return 0;
}
Output:
-6
Explanation (Easy Method):
Given a = 5
Using formula:
~a = -(a + 1)
~5 = -(5 + 1)
= -6
Binary Understanding (Optional):
5 in binary → 00000101
After ~ → 11111010
Since MSB is 1 → number is negative
Find value:
11111010 → 00000101 (flip)
00000101 + 1 = 00000110 → 6
So result = -6
5. Left Shift (<<)
Left Shift (<<) moves the bits of a number
to the left side.
Each shift multiplies the number by 2.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
cout << (a << 1) << endl;
cout << (a << 2);
return 0;
}
Output:
10
20
Explanation:
Binary of 5 → 00000101
5 << 1 (shift by 1) → 00001010 → 10
5 << 2 (shift by 2) → 00010100 → 20
Each shift moves bits to the left and adds 0s at the right.
So, shifting 5 left by 1 gives 10 (5 × 2), and by 2 gives 20
(5 × 4).
6. Right Shift (>>)
Right Shift (>>) moves the bits of a number
to the right side.
Each shift divides the number by 2.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
cout << (a >> 1) << endl;
cout << (a >> 2);
return 0;
}
Output:
2
1
Explanation:
Binary of 5 → 00000101
5 >> 1 (shift by 1) → 00000010 → 2
5 >> 2 (shift by 2) → 00000001 → 1
Each shift to the right divides the number by 2 (ignoring remainder).
5 >> 1 → 5 // 2 = 2
5 >> 2 → 5 // 4 = 1
Where Bitwise Operators are Used
- Fast Execution: Bitwise operations are fast and can replace some arithmetic operations like multiplication/division using shift operators.
- Memory Optimization: Store multiple values in a single variable.
- Graphics & Games: Pixel and color operations.
- Embedded Systems: Hardware control (LEDs, sensors).
- Security: Encryption and data protection.
Summary:
- Bitwise operators work on binary representation of numbers.
- & (AND) → Returns 1 only if both bits are 1.
- | (OR) → Returns 1 if at least one bit is 1.
- ^ (XOR) → Returns 1 if bits are different.
- ~ (NOT) → Flips all bits (1 → 0, 0 → 1).
- << (Left Shift) → Shifts bits to left (multiply by 2).
- >> (Right Shift) → Shifts bits to right (divide by 2).
- These operators are mainly used for low-level operations and performance optimization.