C++ Program To Demonstrate Bitwise Operators

Write C++ code to demonstrate the use of bitwise operators

// Write C++ code to demonstrate the use of bitwise operators.

#include <bits/stdc++.h>
using namespace std ;

int main()
{
     int a = 7,b = 5, c;

     c = a&b;
     cout << "Bitwise AND \t\t: " <<a <<" & " << b <<" = " << c << endl ;

     c = a|b;
     cout << "Bitwise OR \t\t: " <<a <<" | " << b <<" = " << c << endl;

     c = a^b;
     cout << "Bitwise XOR \t\t: " <<a <<" ^ " << b <<" = " << c <<endl ;

     c = ~a;
     cout << "Bitwise COMPLEMENT \t: " << a <<" = " << c <<endl ;

     c = a>>2;
     cout << "Right Shift \t\t: " << a <<" >> 2 " << "= " <<c << endl ;

     c = a<<2;
     cout << "LEFT Shift \t\t: " << a <<" << 2 " << "= " << c <<endl ;

    return 0 ;
}

Output:

Bitwise AND             : 7 & 5 = 5
Bitwise OR              : 7 | 5 = 7
Bitwise XOR             : 7 ^ 5 = 2
Bitwise COMPLEMENT      : 7 = -8
Right Shift             : 7 >> 2 = 1
LEFT Shift              : 7 << 2 = 28

<< Previous | Next >>