Python Guide Sidebar

Python Bitwise Operators

Introduction

Why Study Python bitwise operator?

Python bitwise operators offer a fascinating way to perform operations directly at the bit level. They play a vital role in tasks such as optimizing memory, controlling hardware, and performing fast computations. For careers in data analysis, embedded systems, or competitive programming, mastering bitwise operators provides a distinct edge.

What Will Be Covered?

This guide explores all Python bitwise operators with examples, from simple bit manipulation to real-world use cases. You’ll learn how each operator works, its practical applications, and even some fun examples to help you understand them effortlessly.

Detailed Content

What Are Bitwise Operators in Python?

Bitwise operators allow you to perform operations on individual bits of integers. In Python, these operators are particularly useful for low-level programming, optimizing algorithms, and creating compact data representations.

Python supports the following bitwise operators:

  1. AND (&)
  2. OR (|)
  3. XOR (^)
  4. NOT (~)
  5. Left Shift (<<)
  6. Right Shift (>>)
1.AND (&) Operator

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

Example:

a = 5  # Binary: 0101  
b = 3  # Binary: 0011  
result = a & b  
print(result)  # Output: 1 (Binary: 0001) 

Funny Example:
Imagine you’re ordering pizza and burgers. If both are available (bit 1), you can have them together. Otherwise, you get nothing (0).

2.OR (|) Operator

The OR operator compares each bit of two numbers. If at least one bit is 1, the result is 1.

Example:

a = 5  # Binary: 0101  
b = 3  # Binary: 0011  
result = a | b  
print(result)  # Output: 7 (Binary: 0111)  

Analogy:
Think of a party. If either pizza or burgers are available, the party is on!

3.XOR (^) Operator

The XOR operator gives 1 when the bits are different, and 0 when they’re the same.

Example:

a = 5  # Binary: 0101  
b = 3  # Binary: 0011  
result = a ^ b  
print(result)  # Output: 6 (Binary: 0110) 

Real-Life Example:
It’s like switching a light. Flipping the switch changes its state (on to off or off to on).

4.NOT (~) Operator

The NOT operator inverts all bits, flipping 1 to 0 and vice versa.

Example:

a = 5  # Binary: 0101  
result = ~a  
print(result)  # Output: -6

Explanation:
Python uses two’s complement to represent negative numbers. The result is -(n + 1).

5.Left Shift (<<) Operator

This operator shifts bits to the left, adding zeros on the right. Each shift doubles the number.

Example:

a = 5  # Binary: 0101  
result = a << 1  
print(result)  # Output: 10 (Binary: 1010) 
6.Right Shift (>>) Operator

The right shift moves bits to the right, discarding the bits on the far right.

Example:

a = 5  # Binary: 0101  
result = a >> 1  
print(result)  # Output: 2 (Binary: 0010)  

Summary

  • Bitwise operators manipulate numbers at the bit level.
  • Each operator—AND, OR, XOR, NOT, Left Shift, and Right Shift—has unique behaviors and use cases.
  • Real-world examples like light switches and parties simplify learning.

Learning Outcomes

After this lesson, you will:

  • Understand how bitwise operators work.
  • Use them effectively in programming challenges.
  • Recognize their real-world applications in optimization and hardware control.

Common Interview Questions

1. What are bitwise operators in Python?[Qualcomm]

Answer:
Bitwise operators in Python are used to perform operations at the binary level directly on the bits of integers. They are commonly used for low-level programming, optimization, and hardware interfacing. Python provides the following bitwise operators:

  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)

These operators allow programmers to manipulate data at the bit level, enabling efficient memory usage and faster execution for specific tasks.


2. How does the XOR (^) operator work in Python?[Cisco]

Answer:
The XOR operator compares each bit of two numbers and returns 1 if the bits are different and 0 if they are the same.


3. Explain the difference between left shift (<<) and right shift (>>) operators.[Amazon]

Answer:

  • Left Shift (<<): Shifts all bits of a number to the left by a specified number of positions. Zeros are filled on the right. Each shift multiplies the number by 2
  • Right Shift (>>): Shifts all bits of a number to the right by a specified number of positions. Bits on the far right are discarded. Each shift divides the number by 2.
  • Key Difference:
    Left shift increases the value, while right shift decreases it. Both operations depend on the number of bits shifted.

4. Write a program to swap two numbers without using a temporary variable, utilizing bitwise operators.[Facebook (Meta)]

Answer:
The XOR operator can be used to swap two numbers without requiring extra memory for a temporary variable.

Code Example:

a = 5
b = 3

a = a ^ b  # Step 1: a becomes 6 (Binary: 0110)
b = a ^ b  # Step 2: b becomes 5 (Binary: 0101)
a = a ^ b  # Step 3: a becomes 3 (Binary: 0011)

print("After swapping:")
print("a =", a)  # Output: 3
print("b =", b)  # Output: 5

Explanation:

  1. XOR combines the bits of a and b without losing individual values.
  2. XORing a again with the new result gives b, and similarly for a.

Practice Exercises

  1. Write a program to toggle specific bits in a number.
  2. Create a program to check if a number is even or odd using the AND operator.
  3. Implement a custom bitwise calculator that supports all bitwise operations.

Additional Resources

Now Paly Time