Bitwise Operators

Shrey VohraShrey Vohra
4 min read

🔍 Bitwise Operators in C++ – Explained in Simple Terms

When working with low-level programming or optimization tasks, bitwise operators become super useful. They allow direct manipulation of data at the bit level. Let's break down what these operators are, how they work, and where you might use them.


🧠 What Are Bitwise Operators?

Bitwise operators perform operations on individual bits of integer (whole number) data. In C++, these operators work only with integral data types like int, char, short, long, etc.

Before diving into the types, let’s quickly review what a bit is.

💡 A bit is the smallest unit of data in computing and can either be 0 or 1.
For example, the number 5 in binary is 0101.


🛠️ Types of Bitwise Operators in C++

Here's a summary table first:

OperatorNameSymbolDescription
ANDBitwise AND&Sets each bit to 1 if both bits are 1
ORBitwise OR``
XORBitwise XOR^Sets each bit to 1 if only one bit is 1
NOTBitwise NOT~Inverts all the bits
SHLLeft Shift<<Shifts bits to the left
SHRRight Shift>>Shifts bits to the right

Let’s explore each with examples.


1️⃣ Bitwise AND (&)

This operator compares each bit of two numbers. If both bits are 1, the result is 1; otherwise, it's 0.

t#include <iostream>
using namespace std;

int main() {
    int a = 5;  // binary: 0101
    int b = 3;  // binary: 0011
    int result = a & b; // binary: 0001
    cout << "a & b = " << result; // Output: 1
    return 0;
}

2️⃣ Bitwise OR (|)

This sets each bit to 1 if at least one of the bits is 1.

int a = 5;  // 0101
int b = 3;  // 0011
int result = a | b; // 0111
cout << "a | b = " << result; // Output: 7

3️⃣ Bitwise XOR (^)

Exclusive OR. Sets bit to 1 only if one of the bits is 1, not both.

int a = 5;  // 0101
int b = 3;  // 0011
int result = a ^ b; // 0110
cout << "a ^ b = " << result; // Output: 6

4️⃣ Bitwise NOT (~)

Flips every bit: 1 becomes 0 and 0 becomes 1.

int a = 5;  // 0101
int result = ~a; // depends on the number of bits (usually 32)
cout << "~a = " << result; // Output: -6 (due to two's complement)

⚠️ Note: The result might look confusing because of how negative numbers are represented in memory using two's complement.


5️⃣ Left Shift (<<)

Shifts bits to the left, adding 0s on the right.

int a = 5;      // 0101
int result = a << 1; // 1010 (5 * 2 = 10)
cout << "a << 1 = " << result; // Output: 10

Shifting left by n is like multiplying by 2^n.


6️⃣ Right Shift (>>)

Shifts bits to the right, dropping bits on the right.

int a = 5;      // 0101
int result = a >> 1; // 0010 (5 / 2 = 2)
cout << "a >> 1 = " << result; // Output: 2

Right shifting by n is like dividing by 2^n.


🎯 Real-Life Use Cases of Bitwise Operators

  • Performance Optimization – Faster than arithmetic operations.

  • Flags & Masks – Efficiently store multiple binary states in one integer.

  • Graphics Programming – Working with pixels and color channels.

  • Embedded Systems – Controlling hardware at bit-level.


📌 Summary

ExpressionResult in BinaryDecimal Result
5 & 300011
`53`0111
5 ^ 301106
~5Depends (e.g., 111...1010)-6
5 << 1101010
5 >> 100102
0
Subscribe to my newsletter

Read articles from Shrey Vohra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shrey Vohra
Shrey Vohra