Bitwise operators in C++
Categories:
4 minute read
The provided C++ code demonstrates the use of various bitwise operators.
Code
#include <iostream>
using namespace std;
/**
* Demonstrates the use of bitwise operators in C++.
*
* Bitwise operators used:
* - & (AND)
* - | (OR)
* - ^ (XOR)
* - ~ (NOT)
* - << (LEFT SHIFT)
* - >> (RIGHT SHIFT)
*
* The program performs bitwise operations on two integers and prints the results.
*
* @return int Exit status of the program.
*/
int main() {
int i = 15; // First integer
int j = 22; // Second integer
// Perform bitwise AND operation and print the result
cout << (i & j) << endl; // Expected output: 6
// Perform bitwise OR operation and print the result
cout << (i | j) << endl; // Expected output: 31
// Perform bitwise XOR operation and print the result
cout << (i ^ j) << endl; // Expected output: 25
// Perform bitwise NOT operation on the first integer and print the result
cout << (~i) << endl; // Expected output: -16
// Perform left shift operation on the first integer and print the result
cout << (i << 2) << endl; // Expected output: 60
// Perform right shift operation on the second integer and print the result
cout << (j >> 2) << endl; // Expected output: 5
return 0;
}
Explanation
The provided C++ code demonstrates the use of various bitwise operators. The program begins by including the necessary header file iostream
and using the std
namespace to simplify the code.
#include <iostream>
using namespace std;
The main
function initializes two integer variables, i
and j
, with the values 15 and 22, respectively.
int i = 15; // First integer
int j = 22; // Second integer```
The program then performs several bitwise operations on these integers and prints the results using `cout`.
* **Bitwise AND (`&`)**: This operation compares each bit of `i` and `j` and returns a new integer where each bit is set to 1 only if both corresponding bits of `i` and `j` are 1. The result of `i & j` is 6.
```cpp
cout << (i & j) << endl; // Expected output: 6```
<!-- wp:list {"ordered":true,"start":2} -->
<ol start="2" class="wp-block-list">* **Bitwise OR (`|`)**: This operation compares each bit of `i` and `j` and returns a new integer where each bit is set to 1 if at least one of the corresponding bits of `i` or `j` is 1. The result of `i | j` is 31.
```cpp
cout << (i | j) << endl; // Expected output: 31```
<!-- wp:list {"ordered":true,"start":3} -->
<ol start="3" class="wp-block-list">* **Bitwise XOR (`^`)**: This operation compares each bit of `i` and `j` and returns a new integer where each bit is set to 1 if only one of the corresponding bits of `i` or `j` is 1. The result of `i ^ j` is 25.
```cpp
cout << (i ^ j) << endl; // Expected output: 25```
<!-- wp:list {"ordered":true,"start":4} -->
<ol start="4" class="wp-block-list">* **Bitwise NOT (`~`)**: This operation inverts all the bits of `i`, turning 1s into 0s and vice versa. The result of `~i` is -16.
```cpp
cout << (~i) << endl; // Expected output: -16```
<!-- wp:list {"ordered":true,"start":5} -->
<ol start="5" class="wp-block-list">* **Left Shift (`<<`)**: This operation shifts the bits of `i` to the left by 2 positions, effectively multiplying `i` by 2^2 (or 4). The result of `i << 2` is 60.
```cpp
cout << (i << 2) << endl; // Expected output: 60```
<!-- wp:list {"ordered":true,"start":6} -->
<ol start="6" class="wp-block-list">* **Right Shift (`>>`)**: This operation shifts the bits of `j` to the right by 2 positions, effectively dividing `j` by 2^2 (or 4). The result of `j >> 2` is 5.
```cpp
cout << (j >> 2) << endl; // Expected output: 5```
Finally, the `main` function returns 0, indicating that the program has executed successfully.
```cpp
return 0;
This code provides a clear and concise demonstration of how bitwise operators work in C++, making it a useful reference for developers looking to understand these operations.
Output
6
31
25
-16
60
5
Process finished with exit code 0```