In this article, we will learn about all Bitwise Operators in CPP.
What are Operators?
In programming languages, the operator is a symbol that tells the interpreter/Compiler to perform specific logical, bitwise, relational, or mathematical operations and produce the desired output.
Bitwise Operators are used for manipulating data at the bit level, these operators are used to perform manipulation on individual bits of a number.
Note: The code for all the operators are given at the end of the article
There are different types of bitwise operators in CPP, namely:
- Bitwise AND ( & )
- Bitwise OR ( | )
- Bitwise NOT ( ~ )
- Bitwise XOR ( ^ )
- Left Shift ( << )
- Right shift ( >> )
Let’s look at each one of these in detail.
- The & (bitwise AND) in C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
Symbol: &
Example:
Bitwise AND of numbers 4,6 The binary representation of 4 → 1 0 0 The binary representation of 6 → 1 1 0 = = = Result: 1 0 0 → 4(In decimal) = = =
- The | (bitwise OR) in C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
Symbol: |
Example:
Bitwise OR of numbers 4,6 The binary representation of 4 → 1 0 0 The binary representation of 6 → 1 1 0 = = = Result: 1 1 0 → 6(In decimal) = = =
- The ~ (bitwise NOT) in C++ takes one number and inverts all bits of it
Symbol: ~
Example:
NOT of 7? Binary representation of 7 → 0 1 1 1 Complement: = = = = Result: 1 0 0 0 → 8(In decimal) = = = = Therefore, ~7 = 8
- The ^ (bitwise XOR) in C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
Symbol: ^
Example:
Bitwise XOR of numbers 4,6 The binary representation of 4 → 1 0 0 The binary representation of 6 → 1 1 0 = = = Result: 0 1 0 → 2(In decimal) = = =
- The << (left shift) in C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
The left-shift operator is equivalent to the multiplication of a given number by 2
Note:- It works only if numbers are positive.
- The >> (right shift) in C++ takes two numbers, the right shifts the bits of the first operand, the second operand decides the number of places to shift.
The Right-shift operator is equivalent to a division of the given number by 2
Note:- It works only if numbers are positive.
Code:
C++ Code
#include <bits/stdc++.h>
using namespace std;
int main() {
// a = 5(00000101), b = 9(00001001)
int a = 5, b = 9;
// The result is 00000001
cout << "a = " << a << "," << " b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
// The result is 00001101
cout << "a | b = " << (a | b) << endl;
// The result is 00001100
cout << "a ^ b = " << (a ^ b) << endl;
// The result is 11111010
cout << "~(" << a << ") = " << (~a) << endl;
// The result is 00010010
cout << "b << 1" << " = " << (b << 1) << endl;
// The result is 00000100
cout << "b >> 1 " << "= " << (b >> 1) << endl;
return 0;
}
Output:
a = 5, b = 9
a & b = 1
a | b = 13
a ^ b = 12
~(5) = -6
b << 1 = 18
b >> 1 = 4
Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article
Reference: Bitwise operators in Java