Most high-level language does not allow a bitwise operation because it is a machine level task. The bit is manipulated in memory to get the desired results using bitwise operators.
The list of bitwise operators are as follows
Bitwise Operators | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
<< | Bitwise left shift |
>> | Bitwise right shift |
~ | Complement |
Bitwise AND
A bitwise operation happens between two bits. A number is converted into a bit string and then bitwise operations performed on them.
The
Let
A | B | Output |
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
The only time output is
For example,
\begin{aligned}&1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}0 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}12\\ \\ &0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}7\\ \\ &--------------\\ &0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}0 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}4\\ &--------------\end{aligned}
The result is number
Example Program: Bitwise AND
We now illustrate the correctness of the
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable Declaration unsigned int a, b; unsigned int result; //Variable initialization a = 12; b = 7; result = a & b; //printing the results cout << " Output = " << " " << result << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = 4
Bitwise OR
The
All combination of bit values is given below for
A | B | Output |
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
For example,
If you want to perform
\begin{aligned}&1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}14\\ \\ &0 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}3\\ \\ &--------------\\ &1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}15\\ &--------------\end{aligned}
Therefore, the output is
Example Program: Bitwise OR
To verify the output from above example, we will write a program that computes using
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable declarations int a,b,result; //Variable initialization a = 14; b = 3; result = 0; // Bitwise OR operation result = a | b; //printing the results cout << "Output =" << " " << result << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = 15
The output is what we expected.
Bitwise XOR (Exclusive-OR)
The
All combination of two inputs and a single output is given for
A | B | Output |
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Note how the output is
Let us see a manual example for
If
\begin{aligned}&0 \hspace{5px}0 \hspace{5px}0\hspace{5px}1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}0\hspace{5px}0 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}24\\ \\ &0 \hspace{5px}0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1\hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}45\\ \\ &-------------------\\ &0 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}53\\ &-------------------\end{aligned}
Therefore, the output
Example Program: Bitwise XOR
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable declarations int a,b,result; //Variable initialization a = 24; b = 45; result = 0; // Bitwise OR operation result = a ^ b; //printing the results cout << "Output =" << " " << result << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = 53
Left Shift Operator (<<)
The value of the variable can be modified by changing the bits. This is achieved using bitwise shift operators.
The
For example,
If
\begin{aligned}&0 \hspace{5px}0 \hspace{5px}0\hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}23 \\ \\ &0 \hspace{5px}0 \hspace{5px}1\hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}46 \hspace{5px}after\hspace{5px}1 \hspace{5px}bit \hspace{5px}left\hspace{5px} shift \end{aligned}
Therefore, after shifting a single bit to the left, we get
Example Program: Left Shift
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable Declaration int A; // Variable initialization A = 23; //Bitwise Left Shift A = A << 1; // Printing the results cout << "Output = " << " " << A << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = 46
It is not necessary that you have to shift only one bit, you can shift more than one to archive the desired results.
Right Shift Operator(>>)
The
For example,
If
\begin{aligned}&0 \hspace{5px}0 \hspace{5px}0\hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}23 \\ \\ &0 \hspace{5px}0 \hspace{5px}0\hspace{5px}0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\backslash \backslash Binary \hspace{5px}value\hspace{5px} of \hspace{5px}11\hspace{5px}after\hspace{5px}1 \hspace{5px}bit \hspace{5px}right\hspace{5px} shift \end{aligned}
After shift
Example Program: Right Shift Operator(>>)
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable Declaration int A; // Variable initialization A = 23; //Bitwise Left Shift A = A >> 1; // Printing the results cout << "Output = " << " " << A << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = 11
Complement(~)
The complement inverts the bits of a variable. If
The compiler gives the output in 2’s complement. To convert
\begin{aligned}&1 \hspace{5px}1 \hspace{5px}0\hspace{5px}1 \hspace{5px}0 \hspace{5px}0 \hspace{5px}1 \hspace{5px}1 \hspace{5px}\\ \\ &0 \hspace{5px}0 \hspace{5px}0\hspace{5px}0 \hspace{5px}0 \hspace{5px}0 \hspace{5px}0 \hspace{5px}1 \hspace{5px}\\ &-----------------------\\ &1 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}1 \hspace{5px}0 \hspace{5px}0\hspace{5px}\backslash \backslash Signed \hspace{5px} binary \hspace{5px}value\hspace{5px} of \hspace{5px}-45\\ &----------------------- \end{aligned}
Example Program: Complement
#include <cstdlib> #include <iostream> using namespace std; int main() { //Variable Declaration short int A; // Variable initialization A = 45; //Bitwise Left Shift A = ~A; // Printing the results cout << "Output = " << " " << A << endl; system("PAUSE"); return EXIT_SUCCESS; }
Output:
Output = -45