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.

Advertisements

The list of bitwise operators are as follows

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 works similarly as . All output combination is show for below.

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

ABOutput
111
100
010
000

The only time output is when both and are .

For example, with and 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 .

Example Program: Bitwise AND

We now illustrate the correctness of the 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 is similar to you learned earlier, except that it works at bit level. When a is performed on two bits – and , then the result is if the bit value of or is . The bitwise operations are possible for more than two bits.

All combination of bit values is given below for .

ABOutput
111
101
011
000

For example,

If you want to perform operation on and 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 .

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 is a special kind of logical operation where two single bit inputs – and gives an output of if either of them if is . Otherwise, the result is .

All combination of two inputs and a single output is given for below.

ABOutput
110
101
011
000
Advertisements

Note how the output is when either input is .

Let us see a manual example for and later create a program that performs to verify our output.

If and then 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 is binary equivalent of .

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 modifies the value of variable by shifting the leftmost bits.

For example,

If and you want to change the value of using ,

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

For example,

If 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 bit to the right we get .

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 with binary value then the 1’s complement binary value is which is .

The compiler gives the output in 2’s complement. To convert   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 Detected!!!

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

Exit mobile version