Search⌘ K
AI Features

Bitwise Operators

Learn how bitwise operators work in C++ by processing operands at the binary level. Understand bitwise AND, OR, XOR, left and right shifts, and the complement operator with practical examples. This lesson helps you apply these operators in coding tasks and grasp core binary manipulation concepts.

Introduction

A bitwise operator performs bit by bit processing on the operands.

Bitwise operators operate on binary numbers. They convert operands in decimal form into binary form, perform the particular bitwise operation, and then return the result after converting the number back into decimal form.

Here is the list of bitwise operators available in C++:

Example program with bitwise operators

Consider two operands of type int. The value of operand1 is 3, and the value of operand2 is 2. The program given below demonstrates the use of bitwise operators.

Run the code below and see the output!

C++
#include <iostream>
using namespace std;
int main() {
int operand1 = 3;
int operand2 = 2;
cout << "operand1 = " << operand1 << " , operand2 = " << operand2 << endl;
cout << "operand1 & operand2 = " << (operand1 & operand2) << endl;
cout << "operand1 | operand2 = " << (operand1 | operand2 )<< endl;
cout << "operand1 ^ operand2 = " << (operand1 ^ operand2) << endl;
return 0;
}

Explanation

Let’s convert the given decimal numbers into binary.

Bitwise AND

If the corresponding bits in both the operands is 1, it returns 1. Otherwise, it returns 0.

Bitwise OR

If the corresponding bit in at least of the operands is 1, it returns 1. Otherwise, it returns 0.

Bitwise XOR

If the corresponding bits in both the operands are opposite, it returns 1. Otherwise, it returns 0.

Example program with left and right shift bitwise operators

Consider two operands of type int. The value of operand1 is 2, and the value of operand2 is 1. The program given below demonstrates the use of bitwise operators.

Run the code below and see the output!

C++
#include <iostream>
using namespace std;
int main() {
// your code goes here
int operand1 = 2;
int operand2 = 1;
cout << "operand1 >> operand2 = " << (operand1 >> operand2) << endl;
cout << "operand1 << operand2 = " << (operand1 << operand2) << endl;
return 0;
}

Right shift

Right-shifting an operand1 >> operand2 is equivalent to dividing operand1 by 2operand2.

2>>1=2/21=12 >> 1 = 2/2^1 =1

Left shift

Left-shifting an operand1 << operand2 is equivalent to multiplying operand1 by 2operand2.

2<<1=221=42 << 1 = 2*2^1 =4

~ operator

The tilde operator is a bitwise complement operator that inverts all the bits in a number.

Let’s consider an example in which we have to find 2’s complement of a number. We can do that by inverting all bits in a number and then adding 1 to it. Here’s a program that demonstrates that the 2’s complement of 5 is -5.

C++
#include <iostream>
using namespace std;
int main() {
int operand = 5;
cout << "2's complement of " << operand << " = " << ~operand + 1 << endl;
return 0;
}

2’s complement notation is used by computers to represent negative numbers. Moreover, the ~ operator is also useful for image processing applications where we have to invert bits of an image while applying any mask etc.

Quiz

1.

What is the output of the following code?

int main() {
  
  int operand1 = 6;
  int operand2 = 3;
  cout << "operand1 | operand2 = " << (operand1 | operand2 )<< endl;
  cout << "operand1 ^ operand2 = " << (operand1 ^ operand2) << endl;
  
  return 0;
}
A.

operand1 | operand2 = 7

operand1 ^ operand2 = 5

B.

operand1 | operand2 = 10

operand1 ^ operand2 = 9

C.

operand1 | operand2 = 7

operand1 ^ operand2 = 4

D.

operand1 | operand2 = 3

operand1 ^ operand2 = 2


1 / 1