Bitwise Operators in C++

Bitwise Operators in C++

Bitwise Operators

Bitwise operators let you manage operators at bit level.

  • & (Bitwise and) : It takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

Let do the bitwise AND operation of two integers 2 and 4

2 = 0010 (In Binary)
4 = 0100 (In Binary)
Bit Operation of 2 and 4
  0010
& 0100
  ________
  0000  = 0 (In decimal)

let's work with some code to do crystal clear

#include <iostream>
using namespace std;

int main()
{
    int a = 2, b = 4;
    cout << (a & b);
}

Output

Output = 0
  • | (Bitwise or) : It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1

Let do the bitwise OR operation of two integers 2 and 4

2 = 0010 (In Binary)
4 = 0100 (In Binary)
Bit Operation of 2 and 4
  0010
| 0100
  ________
  0110  = 6 (In decimal)

let's work with some code to do crystal clear

#include <iostream>
using namespace std;

int main()
{
    int a = 2, b = 4;
    cout << (a | b);
}

Output

Output = 6
  • ^ (bitwise XOR) : It takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

Let's do the bitwise XOR operation of two integers 2 and 7

2 = 0010 (In Binary)
7 = 0111 (In Binary)
Bit Operation of 2 and 4
  0010
& 0111
  ________
  0101  = 5 (In decimal)

let's work with some code to do crystal clear

#include <iostream>
using namespace std;

int main()
{
    int a = 2, b = 4;
    cout << (a ^ b);
}

Output

Output = 5
  • ~ (Bitwise not) : it takes one number and inverts all bits of it( 0 becomes 1 and 1 becomes 0).

Let's do the bitwise NOT operation of 35.

35 = 00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011 
  ________
  11011100  = 220 (In decimal)

The bitwise complement of 35 (~35) is -36 instead of 220, but why?

For any integer n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.

2's Complement

Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:

Decimal         Binary           2's complement 
   0            00000000           -(11111111+1) = -00000000 = -0(decimal)
   1            00000001           -(11111110+1) = -11111111 = -256(decimal)
   12           00001100           -(11110011+1) = -11110100 = -244(decimal)
   220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.

Bitwise complement of any number N is -(N+1). Here's how:

bitwise complement of N = ~N (represented

Let's take a look on code :

#include <iostream>
using namespace std;

int main()
{
    int a = 35;
    cout << ~(a);
}

Output

Output : -36

Shift Operators

There are two shift operators :

  • << (left shift) : It takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

Take a look to be clear with concept :

7 = 0111 (In binary)
7<<1 = 1110 (In binary) [Left shift by one bit]
7<<0 = 0111 (Shift by 0)
7<<4 = 0000 (In binary)
  • >> (Right shift) : It takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

Take a look to be clear with concept :

7 = 0111 (In binary)
7<<1 = 1110 (In binary) [Right shift by one bit]
7<<0 = 0111 (Shift by 0)
7<<4 = 0000 (In binary)