In this tutorial, we will learn about different Bitwise Operators available in C++ programming language and go through each of these Bitwise Operations in detail, with the help of examples.
C++ Bitwise Operators
C++ Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.
Operator – Symbol – Example – Description
The following table specifies symbol, example, and description for each of the Assignment Operator in C++.
Bitwise Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | & | x & y | Returns the bitwise AND operation between x and y. |
OR | | | x | y | Returns the bitwise OR operation between x and y. |
XOR | ^ | x ^ y | Returns the bitwise XOR operation between x and y. |
Complement | ~ | ~x | Returns the complement of x. |
Left Shift | << | x << y | Returns the result of x left shifted by y number of places. |
Right Shift | >> | x >> y | Returns the result of x right shifted by y number of places. |
1. Bitwise AND Operator
In the following example, we take two integer values in x
and y
, and find the bitwise AND operation between x
and y
.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 9;
int result = x & y;
cout << "Result : " << result << endl;
}
Output
Result : 1
Program ended with exit code: 0
2. Bitwise OR Operator
In the following example, we take two integer values in x
and y
, and find the bitwise OR operation between x
and y
.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 9;
int result = x | y;
cout << "Result : " << result << endl;
}
Output
Result : 13
Program ended with exit code: 0
3. Bitwise XOR Operator
In the following example, we take two integer values in x
and y
, and find the bitwise XOR operation between x
and y
.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 9;
int result = x ^ y;
cout << "Result : " << result << endl;
}
Output
Result : 12
Program ended with exit code: 0
4. Bitwise Complement Operator
In the following example, we take an integer value in x
, and find the bitwise complement of x
.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int result = ~x;
cout << "Result : " << result << endl;
}
Output
Result : -6
Program ended with exit code: 0
5. Bitwise Left Shift Operator
In the following example, we take two integer values in x
and y
, and find the left shift of x
by y
number of bits.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
int result = x << y;
cout << "Result : " << result << endl;
}
Output
Result : 40
Program ended with exit code: 0
6. Bitwise Right Shift Operator
In the following example, we take two integer values in x
and y
, and find the right shift of x
by y
number of bits.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 49;
int y = 3;
int result = x >> y;
cout << "Result : " << result << endl;
}
Output
Result : 6
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned what Bitwise Operators are, and how to use them in C++ programs, with the help of examples.