Last modified: Oct 28, 2026

Python Bitwise Operators Explained

In Python programming, bitwise operators work directly on binary digits (bits) of integers. These operators perform operations at the bit level, making them powerful tools for low-level manipulation. Whether you are working on embedded systems, cryptography, or performance optimization, understanding these operators is essential.

Bitwise operators treat numbers as sequences of bits. They compare or shift individual bits within these sequences. This approach differs from standard arithmetic operations, which work on entire numbers as values.

Types of Python Bitwise Operators

Python supports six main bitwise operators. Each performs a unique operation on binary representations:

  • AND (&) – Returns 1 if both corresponding bits are 1
  • OR (|) – Returns 1 if at least one corresponding bit is 1
  • XOR (^) – Returns 1 if corresponding bits differ
  • NOT (~) – Flips all bits (unary operator)
  • Left Shift (<<) – Shifts bits left by specified positions
  • Right Shift (>>) – Shifts bits right by specified positions

Let us explore each operator with practical examples and outputs.

1. Bitwise AND Operator (&)

The bitwise AND operator compares each bit of two numbers. If both bits are 1, the result is 1. Otherwise, the result is 0.


# Example: Bitwise AND operation
a = 10        # Binary: 1010
b = 6         # Binary: 0110
result = a & b
print(result) # Output: 2 (Binary: 0010)

2

In this example, only the third bit from the right matches in both numbers. Hence, the result is 2.

2. Bitwise OR Operator (|)

The bitwise OR operator returns 1 if at least one of the corresponding bits is 1. It returns 0 only when both bits are 0.


# Example: Bitwise OR operation
a = 10        # Binary: 1010
b = 6         # Binary: 0110
result = a | b
print(result) # Output: 14 (Binary: 1110)

14

Here, any position where either number has a 1 results in a 1 in the output.

3. Bitwise XOR Operator (^)

The bitwise XOR operator returns 1 when corresponding bits differ. If both bits are the same, it returns 0.


# Example: Bitwise XOR operation
a = 10        # Binary: 1010
b = 6         # Binary: 0110
result = a ^ b
print(result) # Output: 12 (Binary: 1100)

12

XOR is especially useful in algorithms involving toggling or parity checks.

4. Bitwise NOT Operator (~)

The bitwise NOT operator is unary. It flips all bits of its operand. In Python, this follows two's complement representation for negative numbers.


# Example: Bitwise NOT operation
a = 10        # Binary: 1010
result = ~a
print(result) # Output: -11

-11

Remember that ~x equals -(x+1) due to two’s complement logic.

5. Left Shift Operator (<<)

The left shift operator moves all bits to the left by a specified number of positions. Zeros fill the rightmost positions. This effectively multiplies the number by powers of two.


# Example: Left shift operation
a = 10        # Binary: 1010
shift = 2
result = a << shift
print(result) # Output: 40 (Binary: 101000)

40

Shifting 10 left by 2 positions multiplies it by 2², resulting in 40.

6. Right Shift Operator (>>)

The right shift operator moves bits to the right. It fills the leftmost positions with zeros (for positive numbers). This effectively divides the number by powers of two.


# Example: Right shift operation
a = 10        # Binary: 1010
shift = 2
result = a >> shift
print(result) # Output: 2 (Binary: 0010)

2

Shifting 10 right by 2 positions divides it by 2², resulting in 2.

Practical Applications of Bitwise Operators

These operators are not just theoretical concepts. They have real-world applications:

  • Setting or clearing specific flags in configuration files
  • Implementing fast multiplication or division by powers of two
  • Checking whether a number is even or odd using AND with 1
  • Swapping two variables without temporary storage using XOR

For instance, checking if a number is even can be done simply with a & 1. If the result is 0, the number is even.

Important Notes and Best Practices

When working with bitwise operators, consider these key points:

  1. Always ensure operands are integers. Float types will raise errors.
  2. Be cautious with negative numbers and right shifts. Python uses arithmetic shifting.
  3. Use parentheses to clarify complex expressions and avoid precedence issues.
  4. Bitwise operations are faster than arithmetic equivalents. Use them for performance-critical code.

Understanding operator precedence helps avoid bugs. For example, comparison operators bind more tightly than bitwise ones. Always test expressions thoroughly.

Real-World Example: Flag Management

A common use case involves managing multiple boolean flags using a single integer. Each bit represents a different flag.


# Define flag constants
FLAG_READ = 1    # 001
FLAG_WRITE = 2   # 010
FLAG_EXECUTE = 4 # 100

# Set multiple flags
permissions = FLAG_READ | FLAG_WRITE
print(bin(permissions))  # Output: 0b11

# Check if write permission exists
has_write = permissions & FLAG_WRITE
print(has_write)         # Output: 2 (truthy)

# Remove execute permission
permissions &= ~FLAG_EXECUTE
print(bin(permissions))  # Output: 0b11

0b11
2
0b11

This technique saves memory and improves speed in systems programming.

Conclusion

Python bitwise operators provide fine-grained control over binary data. Mastering them enhances your ability to write efficient and expressive code. Practice with small examples to build intuition and confidence.

Remember that while these operators are powerful, clarity matters. Use them where they improve readability and performance. Overuse can make code harder to maintain.

To deepen your Python knowledge, explore related topics such as conditional statements, loops, and variable scoping. Articles like Python If Else Statements: A Beginner Guide, Python For Loop and While Loop Explained, and Python Global and Local Variables offer valuable insights into core programming concepts.