6 Types of bitwise operators in C++
There are 6 Types of bitwise operators in C++. These operators can manipulate bits of a numeric value. These operator are:
**Value is converted into into equivalent binary form before applying bitwise operators.
i. Bitwise AND operator in C++ (&)
& (ampersand sign) represents Bitwise AND operator in C++. If any input of bitwise AND operator is 0, output is 0. If all inputs are 1, output is 1.
Truth table of Bitwise AND operator in C++
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
//Program to demonstrate the use of bitwise and operator.
#include<iostream> using namespace std; int main() { int n1=10,n2=7,n3; n3= n1&n2; cout<<"\nResult=“<<n3; return(0); }
Output:
Result=2
Binary equivalent of n1=10 is 1010 and binary equivalent of n2=7 is 0111.
n1&n2 =0010
Decimal equivalent of 0010 is 2.
ii. Bitwise OR operator in C++ (|)
| (Pipe) represents Bitwise OR operator in C++. If any input of bitwise OR operator is 1, output is 1. If all inputs are 0, output is 0.
Truth table of Bitwise OR operator in C++
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
//Program to demonstrate the use of bitwise OR operator.
#include<iostream> using namespace std; int main() { int n1=10,n2=7,n3; n3=n1|n2; cout<<"\nResult=“<<n3; return(0); }
Output:
Result=15
Binary equivalent of n1=10 is 1010 and binary equivalent of n2=7 is 0111.
n1|n2 =1111
Decimal equivalent of 1111 is 15.
iii. Bitwise XOR operator in C++ (^)
^ (Caret sign) represents Bitwise XOR operator in C++. If odd number of inputs to bitwise XOR are 1, output is 1 otherwise output is 0.
Truth table for Bitwise XOR operator in C++
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
//Program to demonstrate the use of bitwise XOR operator.
#include<iostream> using namespace std; int main() { int n1=10,n2=7,n3; n3=n1|n2; cout<<"\nResult=“<<n3; return(0); }
Output:
Result=13
Binary equivalent of n1=10 is 1010 and binary equivalent of n2=7 is 0111.
n1^n2 =1101
Decimal equivalent of 1111 is 13.
iv. Bitwise complement operator in C++ (~)
! (tild sign) represents Bitwise complement operator in C++. If input to bitwise NOT operator is 1, output is 0, If input to bitwise NOT operator is 0, output is 1,
Truth table for Bitwise Complement operator in C++
Input1 | Output |
0 | 1 |
1 | 0 |
//Program to demonstrate the use of bitwise complement operator.
#include<iostream> using namespace std; int main() { unsigned int n1=4294967295,n2; n2=~n1; cout<<"\nResult="<<n2; return(0); }
Output:
Result=0
Binary equivalent of n1=4294967295 is 11111111111111111111111111111110 and binary equivalent of n2 is 00000000000000000000000000000001.
~n1 =11111111111111111111111111111110
Decimal equivalent of 11111111111111111111111111111110 is 1.
v. Bitwise Left Shift operator in C++ (<<)
<< (Double Less than sign) represents Bitwise Left Shift operator in C++. If shifts the bits towards left by specified number of positions,
//Program to demonstrate the use of bitwise Left Shift operator.
#include<iostream> using namespace std; int main() { int n1=10,n2; n2=(n1<<1); cout<<"\nResult=“<<n2; return(0); }
Output:
Result=20
Binary equivalent of n1=10 is 1010
n1<<1 =10100
Decimal equivalent of 10100 is 20.
vi. Bitwise Right Shift operator in C++ (>>)
>> (Double Greater than sign) represents Bitwise Right Shift operator in C++. If shifts the bits towards right by specified number of positions.
//Program to demonstrate the use of bitwise Right Shift operator.
#include<iostream> using namespace std; int main() { int n1=10,n2; n2=(n1>>1); cout<<"\nResult=“<<n2; return(0); }
Output:
Result=5
Binary equivalent of n1=10 is 1010
n1>>1 =101
Decimal equivalent of 101 is 5.