Introduction
Bitwise operators in C++ are used to perform operations on individual bits of integer data types. These operators are essential for low-level programming, such as system programming, embedded systems, and performance optimization. Understanding bitwise operators allows you to manipulate data at the bit level, providing greater control over hardware and memory.
List of Bitwise Operators
- AND (
&
): Performs a bitwise AND operation. - OR (
|
): Performs a bitwise OR operation. - XOR (
^
): Performs a bitwise XOR (exclusive OR) operation. - NOT (
~
): Performs a bitwise NOT (complement) operation. - Left Shift (
<<
): Shifts bits to the left. - Right Shift (
>>
): Shifts bits to the right.
Bitwise Operators with Examples
Bitwise AND (&
)
The bitwise AND operator compares each bit of its operands. If both bits are 1, the resulting bit is set to 1. Otherwise, it is set to 0.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Binary: 0001
cout << "a & b: " << result << endl; // Output the result
return 0;
}
Output
a & b: 1
Bitwise OR (|
)
The bitwise OR operator compares each bit of its operands. If either bit is 1, the resulting bit is set to 1. Otherwise, it is set to 0.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a | b; // Binary: 0111
cout << "a | b: " << result << endl; // Output the result
return 0;
}
Output
a | b: 7
Bitwise XOR (^
)
The bitwise XOR operator compares each bit of its operands. If the bits are different, the resulting bit is set to 1. Otherwise, it is set to 0.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a ^ b; // Binary: 0110
cout << "a ^ b: " << result << endl; // Output the result
return 0;
}
Output
a ^ b: 6
Bitwise NOT (~
)
The bitwise NOT operator inverts each bit of its operand. It changes 1 to 0 and 0 to 1.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int result = ~a; // Binary: 1010 (in two's complement form, this is -6)
cout << "~a: " << result << endl; // Output the result
return 0;
}
Output
~a: -6
Left Shift (<<
)
The left shift operator shifts the bits of its operand to the left by the specified number of positions. Zero bits are shifted in from the right.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int result = a << 1; // Binary: 1010 (equivalent to 10 in decimal)
cout << "a << 1: " << result << endl; // Output the result
return 0;
}
Output
a << 1: 10
Right Shift (>>
)
The right shift operator shifts the bits of its operand to the right by the specified number of positions. Zero bits are shifted in from the left for unsigned values, and the sign bit is shifted in for signed values.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int result = a >> 1; // Binary: 0010 (equivalent to 2 in decimal)
cout << "a >> 1: " << result << endl; // Output the result
return 0;
}
Output
a >> 1: 2
Combining Bitwise Operators
Bitwise operators can be combined to perform more complex operations.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = (a & b) | (a << 1); // Combining AND and left shift operations
cout << "(a & b) | (a << 1): " << result << endl; // Output the result
return 0;
}
Output
(a & b) | (a << 1): 11
Explanation
-
Bitwise AND (
&
)a & b
: Binary0101 & 0011
results in0001
.
-
Left Shift (
<<
)a << 1
: Binary0101 << 1
results in1010
.
-
Bitwise OR (
|
)(0001 | 1010)
: Binary1011
results in11
in decimal.
Conclusion
Bitwise operators are powerful tools in C++ for performing operations on individual bits. This chapter covered the bitwise AND (&
), OR (|
), XOR (^
), NOT (~
), left shift (<<
), and right shift (>>
) operators with simple examples and their outputs. Understanding these operators will help you manipulate data at the bit level, providing greater control over hardware and memory. In the next chapter, we will explore assignment operators in C++.