Bitwise Operator in C++
In this tutorial, we will learn about bitwise operators in C++. We will implement the concept of bitwise operators with the help of examples.
The operators that alter the bits of a number are known as bitwise operators. Bitwise operators perform binary-level operations on numbers and can be used to set, shift, or delete bits.
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp1.png)
In C++, there are six bitwise operators. These are listed below:
- &: bitwise AND
- |: bitwise OR
- ~: bitwise NOT
- ^: bitwise XOR
- <<: left shift
- >>: right shift
Let us explore each of these bitwise operators in detail with examples.
&: bitwise AND
It accepts two integers as input operands and performs Bitwise AND on the corresponding bits of the two numbers. The bitwise AND operator yields 1 if both operands are 1. Otherwise, it returns a value of zero.
Let us understand the concept of bitwise AND operator with implementing a program in C++.
Code:
#include <iostream>
int main() {
// Declare two variables
unsigned int num1 = 14; // binary: 00001110
unsigned int num2 = 7; // binary: 00000111
// Perform bitwise AND operation
unsigned int result = num1 & num2;
// Display the result
std::cout << "Bitwise AND result: " << result << std::endl;
return 0;
}
Here we have taken two unsigned integer variables, num1, and num2, applied the bitwise operator & to them, and stored the output in another variable named result.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp2.png)
|: bitwise OR
It accepts two integers as input operands and performs Bitwise OR on the corresponding bits of the two numbers. The bitwise OR operator yields 1 if at least one bit is 1. Otherwise, it returns a value of zero.
Let us see an example of a bitwise OR operator.
Code:
#include <iostream>
int main() {
// Declare two variables
unsigned int num1 = 14; // binary: 00001110
unsigned int num2 = 7; // binary: 00000111
// Perform bitwise AND operation
unsigned int result = num1 & num2;
// Display the result
std::cout << "Bitwise AND result: " << result << std::endl;
return 0;
}
In this example, too, we have taken two integer variables, num1, and num2, applied a bitwise OR operator to them, and stored the result in the result variable and displayed it.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp3.png)
~: bitwise NOT
It accepts a single integer as an argument and inverts all the number bits. It signifies that 0 will be replaced by 1 and vice versa.
Let us see an example to understand the bitwise NOT operator in C++.
Code:
#include <iostream>
int main() {
// Declare a variable
unsigned int num = 7; // binary: 00000111
// Perform bitwise NOT operation
unsigned int result = ~num;
// Display the result
std::cout << "Bitwise NOT result: " << result << std::endl;
return 0;
}
Here we have taken an integer variable num and assigned a value 7. Then we applied a bitwise NOT operator with ~ character, stored the result in a variable named result, and displayed the result.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp4.png)
^: bitwise XOR
Bitwise XOR, or Exclusive OR, operates Bitwise XOR on each corresponding bit of two integers as input operands. Bitwise OR yields 1 if the two bits are different. If not, it generates a value of 0.
Let us understand the bitwise XOR operator with the help of a program.
Code:
#include <iostream>
int main() {
// Declare two variables
unsigned int num1 = 14; // binary: 00001110
unsigned int num2 = 7; // binary: 00000111
// Perform a bitwise XOR operation
unsigned int result = num1 ^ num2;
// Display the result
std::cout << "Bitwise XOR result: " << result << std::endl;
return 0;
}
In this program, we have declared two integer variables, num1, and num2, with values 14 and 7, respectively. Then we applied a bitwise XOR operator ^, stored the result in a variable named result, and displayed it.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp5.png)
<<: left shift
As operands, it needs two integers as input. The number of bits specified by the right operand is used to shift the value of the left operand to the left before returning it. If the integers are positive, the left-shift operators are the same as two-fold multiplication.
Take this as an example:
Code:
#include <iostream>
int main() {
// Declare a variable
unsigned int num = 5; // binary: 00000101
// Perform bitwise left shift operation
unsigned int result = num << 3;
// Display the result
std::cout << "Bitwise Left Shift result: " << result << std::endl;
return 0;
}
In this program, we declared a variable named num and assigned a value of 5 to it. Then we applied a bitwise left shift operator to num as num<<3. Here 3 specifies the number of shifts in the bits of num to the left.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp6.png)
>>: right shift
As operands, it needs two integers as input. The number of bits specified by the right operand shifts the value of the left operand to the right. When we shift a number to the right, the least significant bits are lost, and the most significant bits are changed to zeros. The right-shift operators are similar to division by two only if the integers are positive.
Let us better understand the bitwise right shift operator in C++ with the help of an example.
Code:
#include <iostream>
int main() {
// Declare a variable
unsigned int num = 16; // binary: 00010000
// Perform bitwise right shift operation
unsigned int result = num >> 2;
// Display the result
std::cout << "Bitwise Right Shift result: " << result << std::endl;
return 0;
}
Here, we have declared an integer variable named num and assigned a value of 16 to it. We have applied the bitwise right shift operator to num with num>>2, stored the result in a variable named result, and displayed it.
Output:
![Bitwise Operator in C++](https://static.tutorialandexample.com/cpp/bitwise-operator-in-cpp7.png)