In C++, Bitwise Operators are used to perform bit-level operations on integers. These operators treat integers as sequences of binary digits, making them essential for low-level programming, system programming, and performance optimization.
C++ offers six bitwise operators:
- Bitwise AND (
&) - Bitwise OR (
|) - Bitwise XOR (
^) - Bitwise NOT (
~) - Left Shift (
<<) - Right Shift (
>>)
1. Bitwise AND (&)
The Bitwise AND compares each bit of two integers. The result bit is 1 only if both corresponding bits are 1; otherwise, it’s 0.
Example:
7 & 4; // Binary: 111 & 100 = 100 (Decimal: 4)Explanation:
7(binary:111)4(binary:100)- Result:
100(4in decimal).
👉 Learn more about bitwise operations
2. Bitwise OR (|)
The Bitwise OR sets the result bit to 1 if at least one corresponding bit is 1.
Example:
7 | 4; // Binary: 111 | 100 = 111 (Decimal: 7)Explanation:
7(binary:111)4(binary:100)- Result:
111(7in decimal).
3. Bitwise XOR (^)
The Bitwise XOR sets the result bit to 1 if the corresponding bits differ.
Example:
7 ^ 4; // Binary: 111 ^ 100 = 011 (Decimal: 3)Explanation:
7(binary:111)4(binary:100)- Result:
011(3in decimal).
4. Bitwise NOT (~)
The Bitwise NOT flips all bits of a single integer (1 becomes 0, and vice versa).
Example:
~4; // Binary: ~100 = 011 (Decimal: 3)Note: The result depends on the integer size (e.g., 32-bit systems yield -5 due to two’s complement).
5. Left Shift (<<)
The Left Shift moves bits left by specified positions, equivalent to multiplying by 2^n.
Example:
5 << 2; // Binary: 101 << 2 = 10100 (Decimal: 20)Explanation:
5(binary:101) shifted left by2becomes10100(20in decimal).
👉 Explore advanced bit manipulation
6. Right Shift (>>)
The Right Shift moves bits right by specified positions, equivalent to dividing by 2^n.
Example:
16 >> 2; // Binary: 10000 >> 2 = 00100 (Decimal: 4)Explanation:
16(binary:10000) shifted right by2becomes00100(4in decimal).
Practical Code Example
#include <iostream>
using namespace std;
int main() {
int a = 7, b = 4;
cout << "AND: " << (a & b) << endl;
cout << "OR: " << (a | b) << endl;
cout << "XOR: " << (a ^ b) << endl;
cout << "NOT b: " << (~b) << endl;
cout << "Left Shift: " << (5 << 2) << endl;
cout << "Right Shift: " << (16 >> 2) << endl;
return 0;
}Output:
AND: 4
OR: 7
XOR: 3
NOT b: -5
Left Shift: 20
Right Shift: 4FAQs
1. Why use bitwise operators in C++?
Bitwise operators optimize performance in systems programming, cryptography, and hardware interactions by enabling direct bit manipulation.
2. How does Bitwise NOT differ from logical NOT?
Bitwise NOT (~) flips individual bits, while logical NOT (!) evaluates the entire expression as true/false.
3. What’s the difference between << and >>? << multiplies by 2^n, while >> divides by 2^n. Both discard overflow bits.
4. Can bitwise operators work on floats/doubles?
No, they only operate on integer types (int, char, long, etc.).
5. How is XOR useful in programming?
XOR is used in encryption, checksums, and toggling values without temporary variables (e.g., swapping numbers).
Key Takeaways
- Bitwise operators perform low-level bit manipulation.