Last modified: Apr 06, 2026 By Alexander Williams

Python Bitwise Operators Guide & Examples

Bitwise operators are powerful tools in Python. They work directly on the binary bits of integers. This guide explains them in simple terms.

You will learn what each operator does. We will show you practical code examples. This knowledge is key for tasks like data compression and network programming.

What Are Bitwise Operators?

Bitwise operators perform operations on binary numbers. They work bit by bit. Think of them as tools for low-level data manipulation.

They are different from logical operators like and and or. Logical operators work on boolean truth values. Bitwise operators work on the actual bits of integers.

Understanding binary is helpful. But we will keep it simple. You can follow along with basic math.

Core Python Bitwise Operators

Python has six main bitwise operators. Each has a specific symbol and purpose. Let's explore them one by one.

The AND (&) Operator

The bitwise AND operator compares two bits. It returns 1 only if both bits are 1. Otherwise, it returns 0.

It is useful for masking. You can use it to check if specific bits are set. It can also clear certain bits in a number.


# Bitwise AND Example
a = 10  # Binary: 1010
b = 4   # Binary: 0100

result = a & b
print(f"{a} & {b} = {result}")
print(f"Binary: {bin(a)} & {bin(b)} = {bin(result)}")
    

10 & 4 = 0
Binary: 0b1010 & 0b0100 = 0b0
    

The OR (|) Operator

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

This operator is great for combining flags. You can use it to set specific bits to 1 without affecting others.


# Bitwise OR Example
a = 10  # 1010
b = 4   # 0100

result = a | b
print(f"{a} | {b} = {result}")
print(f"Binary: {bin(a)} | {bin(b)} = {bin(result)}")
    

10 | 4 = 14
Binary: 0b1010 | 0b0100 = 0b1110
    

The XOR (^) Operator

The XOR operator is exclusive OR. It returns 1 only if the two bits are different. If they are the same, it returns 0.

This is useful for toggling bits. It is also common in simple encryption and error-checking algorithms.


# Bitwise XOR Example
a = 10  # 1010
b = 4   # 0100

result = a ^ b
print(f"{a} ^ {b} = {result}")
print(f"Binary: {bin(a)} ^ {bin(b)} = {bin(result)}")
    

10 ^ 4 = 14
Binary: 0b1010 ^ 0b0100 = 0b1110
    

The NOT (~) Operator

The bitwise NOT operator flips all the bits. It turns every 1 into a 0 and every 0 into a 1. This is also called the one's complement.

In Python, it works on signed integers. This can lead to results that seem confusing at first. The negative output is due to how Python represents numbers.


# Bitwise NOT Example
a = 10  # Binary: ...00001010

result = ~a
print(f"~{a} = {result}")
print(f"Binary of {a}: {bin(a)}")
print(f"Binary of {result}: {bin(result)}")
    

~10 = -11
Binary of 10: 0b1010
Binary of -11: -0b1011
    

The Left Shift (<<) Operator

The left shift operator moves bits to the left. It adds specified zero bits to the right. Each shift to the left multiplies the number by two.

This is a very fast way to perform multiplication by powers of two. It is a fundamental operation in many computing tasks.


# Left Shift Example
a = 5  # Binary: 0101

result = a << 2  # Shift left by 2 positions
print(f"{a} << 2 = {result}")
print(f"Binary: {bin(a)} << 2 = {bin(result)}")
print(f"Note: {a} * (2**2) = {a * (2**2)}")
    

5 << 2 = 20
Binary: 0b101 << 2 = 0b10100
Note: 5 * (2**2) = 20
    

The Right Shift (>>) Operator

The right shift operator moves bits to the right. It discards bits shifted off the end. Each shift to the right divides the number by two (floor division).

This is a fast way to perform integer division by powers of two. It is the inverse of the left shift operation.


# Right Shift Example
a = 20  # Binary: 10100

result = a >> 2  # Shift right by 2 positions
print(f"{a} >> 2 = {result}")
print(f"Binary: {bin(a)} >> 2 = {bin(result)}")
print(f"Note: {a} // (2**2) = {a // (2**2)}")
    

20 >> 2 = 5
Binary: 0b10100 >> 2 = 0b101
Note: 20 // (2**2) = 5
    

Practical Use Cases for Bitwise Operators

Bitwise operators are not just academic. They solve real-world problems efficiently. Here are some common applications.

1. Checking Odd or Even

You can check if a number is odd or even using AND with 1. If the result is 1, the number is odd. If it's 0, the number is even.


# Check if a number is odd or even
def is_odd(num):
    return (num & 1) == 1

print(f"Is 7 odd? {is_odd(7)}")
print(f"Is 12 odd? {is_odd(12)}")
    

Is 7 odd? True
Is 12 odd? False
    

2. Setting, Clearing, and Toggling Bits

You can manipulate specific bits within a number. This is often called bit masking. It's essential for working with hardware registers or packed data.


# Bit Manipulation Examples
flags = 0b0000  # Start with all bits off

# Set the 3rd bit (from right, 0-indexed) to 1
flags = flags | (1 << 2)  # Use OR with a mask
print(f"Set bit 2: {bin(flags)}")

# Clear the 1st bit (from right) to 0
flags = flags & ~(1 << 0) # Use AND with inverted mask
print(f"Clear bit 0: {bin(flags)}")

# Toggle the 2nd bit
flags = flags ^ (1 << 1)  # Use XOR with a mask
print(f"Toggle bit 1: {bin(flags)}")
    

Set bit 2: 0b100
Clear bit 0: 0b100
Toggle bit 1: 0b110
    

3. Simple Encryption (XOR Cipher)

The XOR operator has a unique property. Applying XOR twice with the same key returns the original data. This allows for basic encryption.


# Simple XOR Encryption/Decryption
def xor_cipher(data, key):
    return data ^ key

message = 42
secret_key = 123

# Encrypt
encrypted = xor_cipher(message, secret_key)
print(f"Original: {message}, Encrypted: {encrypted}")

# Decrypt
decrypted = xor_cipher(encrypted, secret_key)
print(f"Encrypted: {encrypted}, Decrypted: {decrypted}")
    

Original: 42, Encrypted: 81
Encrypted: 81, Decrypted: 42
    

Important Notes and Best Practices

Bitwise operators are powerful but require care. Keep these points in mind.

Operator Precedence: Bitwise operators have lower precedence than arithmetic operators. Use parentheses to make your intentions clear.

Working with Negative Numbers: Remember that Python uses two's complement for negative integers. The NOT (~) operator's result reflects this.

Readability: Use bitwise operators when they are the best tool. For simple checks, logical operators are often clearer. Always add comments to explain complex bit manipulations.

Conclusion

Python bitwise operators give you direct control over binary data. They are essential for system-level programming, cryptography, and performance-critical code.

You learned about AND, OR, XOR, NOT, and shift operators. We explored practical examples like checking parity and bit masking.

Start by practicing with small numbers. Use the bin() function to see the binary representation. This will build your intuition.

Mastering these operators unlocks a deeper understanding of how computers work. It makes you a more versatile Python programmer.