Bitwise Operators in C++

Bitwise Operators perform operations on the binary (bits) representation of numbers. These operators work directly on the bits of integers.

To understand bitwise operators easily, numbers are first converted into binary form and then the operation is performed on each bit.

Let us take two numbers:

a = 5   →  0101
b = 3   →  0011

List of Bitwise Operators

Operator Name Description Example
& Bitwise AND Returns 1 if both bits are 1 5 & 3 → 1
| Bitwise OR Returns 1 if at least one bit is 1 5 | 3 → 7
^ Bitwise XOR Returns 1 if bits are different 5 ^ 3 → 6
~ Bitwise NOT Inverts all bits ~5 → -6
<< Left Shift Shifts bits to the left 5 << 1 → 10
>> Right Shift Shifts bits to the right 5 >> 1 → 2

Examples of Bitwise Operators

Let us take two numbers:

int a = 5;      // binary: 0101
int b = 3;      // binary: 0011

1. Bitwise AND (&)

Bitwise AND (&) compares each bit of two numbers and returns 1 only when both bits are 1. Otherwise the result becomes 0.

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 3;

    cout << (a & b);

    return 0;
}

Output:

1

Explanation:

  0101
& 0011
-------
  0001 → 1

2. Bitwise OR (|)

Bitwise OR (|) compares each bit of two numbers and returns 1 if at least one bit is 1.

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 3;

    cout << (a | b);

    return 0;
}

Output:

7

Explanation:

  0101
| 0011
-------
  0111 → 7

3. Bitwise XOR (^)

Bitwise XOR (^) returns 1 when the bits are different, and returns 0 when the bits are same.

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 3;

    cout << (a ^ b);

    return 0;
}

Output:

6

Explanation:

  0101
^ 0011
-------
  0110 → 6

4. Bitwise NOT Operator (~)

Bitwise NOT (~) is a unary operator that reverses all the bits of a number. It converts every 1 into 0 and every 0 into 1.

This operator works using the concept of 2’s complement, so the result is usually a negative number.

Shortcut Rule (Very Important)

~a = -(a + 1)

Instead of converting into binary every time, you can use this simple formula to find the result quickly.

Example

#include <iostream>
using namespace std;

int main()
{
    int a = 5;

    cout << ~a;

    return 0;
}

Output:

-6

Explanation (Easy Method):

Given a = 5

Using formula:
~a = -(a + 1)

~5 = -(5 + 1)
   = -6

Binary Understanding (Optional):

5 in binary  → 00000101
After ~      → 11111010

Since MSB is 1 → number is negative

Find value:
11111010 → 00000101 (flip)
00000101 + 1 = 00000110 → 6

So result = -6

5. Left Shift (<<)

Left Shift (<<) moves the bits of a number to the left side. Each shift multiplies the number by 2.

#include <iostream>
using namespace std;

int main()
{
    int a = 5;

    cout << (a << 1) << endl;
    cout << (a << 2);

    return 0;
}

Output:

10

20

Explanation:

Binary of 5      →  00000101
5 << 1 (shift by 1) → 00001010 → 10
5 << 2 (shift by 2) → 00010100 → 20

Each shift moves bits to the left and adds 0s at the right. So, shifting 5 left by 1 gives 10 (5 × 2), and by 2 gives 20 (5 × 4).

6. Right Shift (>>)

Right Shift (>>) moves the bits of a number to the right side. Each shift divides the number by 2.

#include <iostream>
using namespace std;

int main()
{
    int a = 5;

    cout << (a >> 1) << endl;
    cout << (a >> 2);

    return 0;
}

Output:

2

1

Explanation:

Binary of 5         →  00000101
5 >> 1 (shift by 1) → 00000010 → 2
5 >> 2 (shift by 2) → 00000001 → 1

Each shift to the right divides the number by 2 (ignoring remainder).

5 >> 1 → 5 // 2 = 2

5 >> 2 → 5 // 4 = 1

Where Bitwise Operators are Used

  • Fast Execution: Bitwise operations are fast and can replace some arithmetic operations like multiplication/division using shift operators.
  • Memory Optimization: Store multiple values in a single variable.
  • Graphics & Games: Pixel and color operations.
  • Embedded Systems: Hardware control (LEDs, sensors).
  • Security: Encryption and data protection.

Summary:

  1. Bitwise operators work on binary representation of numbers.
  2. & (AND) → Returns 1 only if both bits are 1.
  3. | (OR) → Returns 1 if at least one bit is 1.
  4. ^ (XOR) → Returns 1 if bits are different.
  5. ~ (NOT) → Flips all bits (1 → 0, 0 → 1).
  6. << (Left Shift) → Shifts bits to left (multiply by 2).
  7. >> (Right Shift) → Shifts bits to right (divide by 2).
  8. These operators are mainly used for low-level operations and performance optimization.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar