Bitwise operators
Bitwise operators in C++ allow you to perform operations directly on the binary representations of integers. These operators are useful for tasks that require manipulation of individual bits within a byte or word, such as in low-level programming, cryptography, and performance optimization.
Types of Bitwise Operators
C++ provides several bitwise operators:
Bitwise AND (&)
- Description: Performs a bitwise AND operation on each bit of the operands. The result is 1 only if both corresponding bits are 1.
- Example: a & b compares each bit of a and b and results in 1 only where both bits are 1.
Bitwise OR (|)
- Description: Performs a bitwise OR operation on each bit of the operands. The result is 1 if at least one of the corresponding bits is 1.
- Example: a | b compares each bit of a and b and results in 1 if either bit is 1.
Bitwise XOR (^)
- Description: Performs a bitwise XOR (exclusive OR) operation on each bit of the operands. The result is 1 if the corresponding bits are different.
- Example: a ^ b results in 1 where the bits of a and b differ.
Bitwise NOT (~)
- Description: Inverts all the bits of the operand, turning 1s into 0s and 0s into 1s.
- Example: ~a flips all the bits in a.
Left Shift (<<)
- Description: Shifts the bits of the left-hand operand to the left by the number of positions specified by the right-hand operand. Vacated bits are filled with 0.
- Example: a << 2 shifts the bits in a two positions to the left.
Right Shift (>>)
- Description: Shifts the bits of the left-hand operand to the right by the number of positions specified by the right-hand operand. For unsigned types, vacated bits are filled with 0. For signed types, the behavior depends on the implementation (usually sign-extension).
- Example: a >> 2 shifts the bits in a two positions to the right.
Example Code
Let’s look at an example to understand how these operators work in practice:
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int b = 9; // Binary: 1001
// Bitwise AND
int result = a & b; // Binary: 0001, Decimal: 1
cout << "a & b = " << result << endl;
// Bitwise OR
result = a | b; // Binary: 1101, Decimal: 13
cout << "a | b = " << result << endl;
// Bitwise XOR
result = a ^ b; // Binary: 1100, Decimal: 12
cout << "a ^ b = " << result << endl;
// Bitwise NOT
result = ~a; // Binary: 11111111111111111111111111111010 (in 32-bit signed integer representation), Decimal: -6
cout << "~a = " << result << endl;
// Left Shift
result = a << 1; // Binary: 1010, Decimal: 10
cout << "a << 1 = " << result << endl;
// Right Shift
result = b >> 1; // Binary: 0100, Decimal: 4
cout << "b >> 1 = " << result << endl;
return 0;
}
Explanation of the Example
Bitwise AND (a & b):
- 5 & 9 results in 1 because the only position where both 0101 (5) and 1001 (9) have 1 is the least significant bit.
Bitwise OR (a | b):
- 5 | 9 results in 13 because the result is 1101, where any bit that is 1 in either operand results in 1.
Bitwise XOR (a ^ b):
- 5 ^ 9 results in 12 because the result is 1100, where bits are different between a and b.
Bitwise NOT (~a):
- ~5 results in -6. The bitwise NOT inverts all bits, but when interpreted as a signed integer, the result is -6.
Left Shift (a << 1):
- Shifting 5 left by 1 position results in 10 (binary 1010), effectively multiplying 5 by 2.
Right Shift (b >> 1):
- Shifting 9 right by 1 position results in 4 (binary 0100), effectively dividing 9 by 2.
Conclusion
Bitwise operators are powerful tools in C++ that allow you to manipulate individual bits within an integer. They are often used in systems programming, hardware interfacing, and optimizing performance-sensitive code. By understanding and practicing with these operators, you can perform low-level data manipulation efficiently in your C++ programs.