Bitwise Operators in C or C++

In this tutorial, we will learn about Bitwise Operators in C or C++.

Bitwise Operators in C or C++

Bit manipulation means to algorithmically make changes in the bits of literals. The operators we use to do these manipulations are called Bitwise Operators. In C/C++, we have six types of bitwise operators, namely bitwise AND (&), bitwise OR (|), bitwise NOT (~), bitwise XOR (^), left shift (<<) and right shift (>>).

Bitwise AND (&) Operator

It takes two numbers as operands and does AND operation on every bit of two numbers. The result of AND is 1 only if both bits are 1.

X Y X & Y
0 0 0
0 1 0
1 0 0
1 1 1

Program to show Bitwise AND Operator in C++

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int bit(int c)						//to convert into binary number
{
  int r, b = 0, i = 0;
  while (c != 0)
  {
    r = c % 2;
    c = c / 2;
    b += r * pow(10,i);
    i++;
  }
  return b;
}
int main()
{
  int a = 2, b = 7, c;
  cout << "a = 2 \t a = 0010\n";
  cout << "b = 7 \t b = 0111\n";
  c = a & b;                      //bitwise operation
  cout << "c = " << c ;
  int d = bit(c);
  cout << "\t c = " <<setfill('0')<<setw(4)<< d;    //to set the width
  
  return 0;
}

Output :

a = 2  a = 0010
b = 7  b = 0111
c = 2  c = 0010

Bitwise OR (|) Operator in C++

It takes two numbers as operands and does OR operation on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

X Y X | Y
0 0 0
0 1 1
1 0 1
1 1 1

Program to show Bitwise OR

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int bit(int c)						//to convert into binary number
{
  int r, b = 0, i = 0;
  while (c != 0)
  {
    r = c % 2;
    c = c / 2;
    b += r * pow(10,i);
    i++;
  }
  return b;
}
int main()
{
  int a = 2, b = 7, c;
  cout << "a = 2 \t a = 0010\n";
  cout << "b = 7 \t b = 0111\n";
  c = a | b;                      //bitwise operation
  cout << "c = " << c ;
  int d = bit(c);
  cout << "\t c = " <<setfill('0')<<setw(4)<< d;    //to set the width
  
  return 0;
}

Output :

a = 2  a = 0010
b = 7  b = 0111
c = 7  c = 0111

Bitwise NOT (~)

It takes one number and inverts all bits of it. That is if the bit reads 0, it inverts it to 1. Similarly, it inverts 1 to 0. Here, we have the table depicting the NOT operator.

X ~X
1 0
0 1

Program to show Bitwise NOT

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
  int a = 2, c;
  cout << "a = 2\n";        // 2 = 0010
  c = ~a;                      //bitwise operation
  cout << "c = " << c ;     //~a = -3 = 1101
  return 0;
}

Output :

a = 2
c = -3

Bitwise XOR (^)

It takes two numbers as operands and does XOR operation on every bit of two numbers. The result of XOR is 1 if the two bits are different.

X Y X ^ Y
0 0 0
0 1 1
1 0 1
1 1 0

Program to show Bitwise XOR

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int bit(int c)						//to convert into binary number
{
  int r, b = 0, i = 0;
  while (c != 0)
  {
    r = c % 2;
    c = c / 2;
    b += r * pow(10,i);
    i++;
  }
  return b;
}
int main()
{
  int a = 2, b = 7, c;
  cout << "a = 2 \t a = 0010\n";
  cout << "b = 7 \t b = 0111\n";
  c = a ^ b;                      //bitwise operation
  cout << "c = " << c ;
  int d = bit(c);
  cout << "\t c = " <<setfill('0')<<setw(4)<< d;    //to set the width
  
  return 0;
}

Output :

a = 2  a = 0010
b = 7  b = 0111
c = 5  c = 0101

Left shift (<<) Operator in C++

It takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Here, we have the table depicting the left shift operator.

X 0 0 1 0
X<<1 0 1 0 0

Program to show left shift in C++

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int bit(int c)						//to convert into binary number
{
  int r, b = 0, i = 0;
  while (c != 0)
  {
    r = c % 2;
    c = c / 2;
    b += r * pow(10,i);
    i++;
  }
  return b;
}
int main()
{
  int a = 2, c;
  cout << "a = 2 \t a = 0010\n";
  c = a<<1;                      //bitwise operation
  cout << "c = " << c ;
  int d = bit(c);
  cout << "\t c = " <<setfill('0')<<setw(4)<< d;    //to set the width
  
  return 0;
}

Output :

a = 2  a = 0010
c = 4  c = 0100

Right shift(>>) Operator in C++

It takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. Here, we have the table depicting the right shift operator.

X 0 0 1 0
X>>1 0 0 0 1

Program to show right shift in C++

# include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int bit(int c)						//to convert into binary number
{
  int r, b = 0, i = 0;
  while (c != 0)
  {
    r = c % 2;
    c = c / 2;
    b += r * pow(10,i);
    i++;
  }
  return b;
}
int main()
{
  int a = 2, c;
  cout << "a = 2 \t a = 0010\n";
  c = a>>1;                      //bitwise operation
  cout << "c = " << c ;
  int d = bit(c);
  cout << "\t c = " <<setfill('0')<<setw(4)<< d;    //to set the width
  
  return 0;
}

Output :

a = 2  a = 0010
c = 1  c = 0001

Hope this was helpful. Enjoy Coding!

Also learn:

Break and Continue in C++

Taking only integer input in C++

Leave a Reply

Your email address will not be published. Required fields are marked *