5. Bitwise Operators:
The Bitwise operators are used to perform bit manipulation on numbers. There are various types of Bit operators that are used in C++.
i) Bitwise AND operator (&): It 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. Mind that the commutative property holds true here.
That is,
1 & 1 = 1
1 & 0 = 0
Example:
#include<iostream>
using namespace std;
int main() {
int a = 6; // Binary representation of 6 is 0110
int b = 7; // Binary representation of 7 is 0111
cout << ”a & b = ” << (a & b); //0110 & 0111 = 0110 = 6
return 0;
}
Output:
a & b = 6
ii) Bitwise OR operator (|): It 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. Mind that the commutative property holds true here.
That is,
1 | 1 = 1
1 | 0 = 1
0 | 0 = 0
Example:
#include<iostream>
using namespace std;
int main() {
int a = 6; // Binary representation of 6 is 0110
int b = 7; // Binary representation of 7 is 0111
cout << "a | b = " << (a | b); //0110 | 0111 = 0111 = 7
return 0;
}
Output:
a | b = 7
iii) Bitwise NOT operator (~): It takes one number and inverts all bits of it.
That is,
~1 = 0
~0 = 1
Example:
#include<iostream>
using namespace std;
int main() {
// Binary representation of 6 is 00000000000000000000000000000110 (size of int is 32 bits)
int a = 6;
// ~6 = 11111111111111111111111111111001 = -7 (-ve nos. are stored in 2's complement form)
cout << "~a = " << (~a);
return 0;
}
Output:
~a = -7
iv) Bitwise XOR operator (^): It 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. Mind that the commutative property holds true here.
That is,
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 0 = 0
Example:
#include<iostream>
using namespace std;
int main() {
int a = 6; // Binary representation of 6 is 0110
int b = 7; // Binary representation of 7 is 0111
cout << "a ^ b = " << (a ^ b); //0110 ^ 0111 = 0001 = 1
return 0;
}
Output:
a ^ b = 1
v) Left shift operator (<<): It takes two numbers, the left shift operator shifts the bits of the first operand, the second operand decides the number of places to shift.
Example:
#include<iostream>
using namespace std;
int main() {
int a = 8; // Binary representation of 8 is 1 0 0 0
cout << "a << 2 = " << (a << 2);
// Left shift means appending numbers of 0’s to the right.
//1 0 0 0 0 0 = 32
return 0;
}
Output:
a << 2 = 32
vi) Right shift operator (>>): It takes two numbers; the right shift operator shifts the bits of the first operand, the second operand decides the number of places to shift.
Example:
#include<iostream>
using namespace std;
int main() {
int a = 8; // Binary representation of 8 is 1 0 0 0
cout << "a >> 2 = " << (a >> 2);
// Right shift means remove numbers of 0’s from right 1 0 = 2
return 0;
}
Output:
a >> 2 = 2
NOTE: The left shift and right shift operators should not be used for negative numbers. If any of the operands is a negative number, it results in undefined behaviour. For example results of both -1 << 1 and 1 << -1 is undefined. Also, if the number is shifted more than the integer’s size, the behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits.