5. Bitwise Operators

Bitwise operators perform operations on the binary representation of numbers. These are used to manipulate individual bits of data. They work only on integers, and the result is also an integer.

Applying a bitwise operation like AND (&) on two numbers i.e a=5, b=3 gives:

  a = 5    →  0101
  b = 3    →  0011
  -------------------
  a & b    →  0001 → Output: 1

List of Bitwise Operators

Bitwise operators perform operations on bits (binary digits) of numbers. These are mainly used in low-level programming, memory operations, and performance-critical applications.

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 by specified number of positions 5 << 1 → 10
>> Right Shift Shifts bits to the right by specified number of positions 5 >> 1 → 2

Examples of Bitwise Operators

Let's take two numbers:

a = 5      # in binary: 0101
b = 3      # in binary: 0011

1. Bitwise AND (&)

Bitwise AND (&) compares each bit of two numbers and returns 1 only if both corresponding bits are 1. Otherwise, it returns 0. It performs the logical "AND" operation at the bit level.

print(a & b)     # 1

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 of the bits is 1. It performs the logical "OR" operation on each pair of corresponding bits.


print(a | b)     # 7
    

Output:

7

Explanation:

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

3. Bitwise XOR (^)

Bitwise XOR (^) compares each bit of two numbers and returns 1 if the bits are different (i.e., one is 1 and the other is 0). If both bits are the same, it returns 0. It performs the exclusive OR operation.

print(a ^ b)     # 6

Output:

6

Explanation:

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

4. Bitwise NOT (~)

Bitwise NOT (~) is a unary operator that inverts each bit of the number. It changes every 1 to 0 and every 0 to 1. In Python, it returns the **two's complement** of the number, i.e., ~x = -(x + 1).

print(~a)     # -6

Output:

-6

Explanation:

Binary of 5  →  00000101
~5           →  11111010 (inverted)
             →  -6 (Two's complement)

So, ~5 means all bits are flipped, and Python treats it as -(5 + 1) = -6.

5. Left Shift (<<)

Bitwise Left Shift (<<) shifts the bits of a number to the left by a specified number of positions. Each left shift multiplies the number by 2 for every shift position.

print(a << 1)     # 10
print(a << 2)     # 20

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 (>>)

Bitwise Right Shift (>>) shifts the bits of a number to the right by a specified number of positions. Each right shift divides the number by 2 for every shift position, discarding the remainder.

print(a >> 1)     # 2
print(a >> 2)     # 1

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

Why Use Bitwise Operators?

  1. Fast Operations: Bitwise operations are faster than arithmetic operations.
  2. Low-Level Programming: Common in hardware programming, device drivers, and embedded systems.
  3. Memory Efficiency: Useful in saving space, like storing flags or multiple states in a single variable.
  4. Cryptography: Used in encryption, compression, hashing.
  5. Permission Handling: Flags like Read (0001), Write (0010), Execute (0100) can be handled efficiently.

Where are Bitwise Operators Used?

  • Graphics programming (like pixel-level work)
  • Encryption algorithm and hashing
  • Handling flags and permissions
  • Memory-efficient programming

Summary

  • & - AND: 1 if both bits are 1
  • | - OR: 1 if any bit is 1
  • ^ - XOR: 1 if bits are different
  • ~ - NOT: Inverts all bits
  • << - Left Shift: Multiplies by 2
  • >> - Right Shift: Divides by 2

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