In this tutorial, we are going to explore bitwise operators in C++. As the name says, here, we are going to do operations on bits(binary digits). We will see what are some symbols related to bitwise operators in C++, and we will also go through a simple example, to understand the working of these operators.
Bitwise operators in C++
|&||This operator performs AND operation between the two values||For operations 5&3, the output is 1, as the AND operation between 101 and 011 gives the value 001, which is a binary of 1.|
||||This operator performs OR operation between the two values||For an operation 5 | 3, the output is 7, as the OR operation between 101 and 011 gives the value 111, which is a binary of 7.|
|^||This operator performs an XOR operation between the two values||For an operation 5 ^3, the output is 6, as the XOR operation between 101 and 011 gives the value 110, which is a binary of 6.|
|~||Bitwise complement operator||For an operation ~5, the output is -6|
|<<||This operator performs a left shift on the value by a specified number of bits.||8<<2 (read as 8 left shift by 2 bits), which means that we are shifting the bits which describe 8(1000), towards left by two bits, so it becomes 100000, which is 32.|
|>>||This operator performs a right shift on the value by a specified number of bits.||10>>1 (read as 10 right shift by 1), which means that we are shifting the bits which describe 10 (1010), towards the right by 1 bit, so it becomes 0101, which is 5.|
As you can see from the above table, we have listed some of the bitwise operators used in C++. Now, let’s have a look at a simple program, through which, we will try to demonstrate the working of bitwise operators, and we will try to use them.
int a = 10, b = 2;
std::cout << “a & b: “<< (a&b) << std::endl;
std::cout << “a | b: “<< (a|b) << std::endl;
std::cout << “a ^ b: “<< (a^b) << std::endl;
std::cout << “~a: “<< (~a) << std::endl;
std::cout << “a << 2: “<< (a <<2) << std::endl;
std::cout << “a >> 2: “<< (a >> 2) << std::endl;
As you can see in the above program, we have tried to use the bitwise operators. Remember that the operations here are done on bits. Here is the output of the above program.
a & b: 2
a | b: 10
a ^ b: 8
a << 2: 40
a >> b: 2
As you can see from the output, we could perform the operations on the bits, using the bitwise operators. You can use the bitwise operators as and when required in your C++ programs. Please keep a note of this, with the bitwise operators, we are doing operations on bits. At times, you might want to use the bitwise operators in your programs.
If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to Python, Machine learning, and Data Science, through which, you can upskill yourself.
FAQs related to bitwise operators in C++
Q: What are bitwise operators?
Ans: Using the bitwise operators, we can perform operations on the bits. There are different bitwise operator symbols available for different bitwise operations.
Q: What are some bitwise operator symbols?
Ans: Here are some bitwise operator symbols in C++ (&, |, ^, ~, >>, <<)