C++ Bitwise Operators

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

Advertisements
Bitwise OperatorsDescription
&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 bitwise \hspace{3px} AND works similarly as logical \hspace{3px} AND. All output combination is show for bitwise \hspace{3px} AND below.

Let A and B be two variables with some one bit value, then:

ABOutput
111
100
010
000

The only time output is 1 when both A and B are 1.

For example, bitwise \hspace{3px} AND with 12 and 7 will result in following:

\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 4.

Example Program: Bitwise AND

We now illustrate the correctness of the bitwise \hspace{3px}AND through a program.

#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 bitwise \hspace{3px} OR is similar to logical \hspace{3px} OR you learned earlier, except that it works at bit level. When a bitwise \hspace{3px}OR is performed on two bits – A and B, then the result is 1 if the bit value of A or B is 1. The bitwise operations are possible for more than two bits.

All combination of bit values is given below for bitwise \hspace{3px} OR.

ABOutput
111
101
011
000

For example,

If you want to perform bitwise \hspace{3px} OR operation on A = 14 and B = 3 then

\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 15.

Example Program: Bitwise OR

To verify the output from above example, we will write a program that computes using bitwise \hspace{3px} OR.

#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 bitwise \hspace{3px} XOR is a special kind of logical operation where two single bit inputs – A and B gives an output of 1 if either of them if is 1. Otherwise, the result is 0.

All combination of two inputs and a single output is given for bitwise \hspace{3px} XOR below.

ABOutput
110
101
011
000
Advertisements

Note how the output is 1 when either input is 1.

Let us see a manual example for bitwise \hspace{3px} XOR and later create a program that performs bitwise \hspace{3px} XOR to verify our output.

If A = 24 and B = 45 then bitwise \hspace{3px} XOR will result in following result.

\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 0011 \hspace{2px} 0101 is binary equivalent of 53.

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 left \hspace{3px}shift\hspace{3px} operator modifies the value of variable by shifting the leftmost bits.

For example,

If A = 23 and you want to change the value of A using left \hspace{3px}shift\hspace{3px} operator ,

\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 46.

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 right\hspace{3px} shift \hspace{3px}operator is similar to left shift operator and it modify the value of a variable by shifting the rightmost bits.

For example,

If A = 23 and you want to shift rightmost bit then,

\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 1 bit to the right we get 11.

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 A = 44 with binary value 0010 \hspace{3px}1100 then the 1’s complement binary value is 1101 \hspace{3px}0011 which is 128 + 64 + 16 + 3 = 211.

The compiler gives the output in 2’s complement. To convert 1101 \hspace{3px}0011  into 2’s complement.

\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

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.