Back to Python
2026-03-268 min read

5. Python Bitwise operators

Learn 5. Python Bitwise operators step by step with clear examples and exercises.

Title: Mastering Python Bitwise Operators - A Deep Dive into Binary Arithmetic for Efficient Coding

Why This Matters

In this comprehensive tutorial, we will delve deep into the captivating world of Python bitwise operators. These operators are indispensable for efficient coding, especially when dealing with low-level programming tasks and algorithmic challenges. Understanding them can help you solve complex problems, optimize your code, and even prepare for interviews and exams that require a profound understanding of binary arithmetic.

Python's bitwise operators allow for manipulation of individual bits within integers. They enable us to perform operations on the binary representation of numbers, which can be incredibly useful for various programming tasks. This tutorial will cover the following Python bitwise operators:

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

Prerequisites

Before diving into bitwise operators, it's crucial to have a solid foundation in Python programming concepts:

  1. Basic Python syntax and data types (variables, constants, strings, lists, etc.)
  2. Control structures (if-else statements, for loops, while loops)
  3. Understanding of variables and their memory allocation
  4. Familiarity with arithmetic operators (+, -, *, /, %, )
  5. Adequate understanding of Python's data types, including integers and floating-point numbers
  6. Knowledge of Python's conditional expressions (x if condition else y)
  7. Understanding of basic binary arithmetic, including the binary representation of numbers

Core Concept

Bitwise operators in Python manipulate individual bits within integers. They allow you to perform operations on the binary representation of numbers, which can be incredibly useful for various programming tasks. Here's a list of Python's bitwise operators:

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

Understanding Binary Representation

Every integer can be represented in binary, a base-2 number system using 0s and 1s. For example, the decimal number 5 is represented as 101 in binary. Each digit represents a power of 2:

  • The rightmost digit (power of 2^0) corresponds to 1
  • The second digit from the right (power of 2^1) corresponds to 2
  • The third digit from the right (power of 2^2) corresponds to 4, and so on.

Bitwise AND (&)

The bitwise AND operator compares each corresponding bit in two integers and produces a 1 only if both bits are 1. For example:

a = 6 # binary: 110
b = 3 # binary: 011
a & b # binary: 010 => decimal: 2

Bitwise OR (|)

The bitwise OR operator produces a 1 if either of the corresponding bits in two integers is 1. For example:

a = 6 # binary: 110
b = 3 # binary: 011
a | b # binary: 111 => decimal: 7

Bitwise XOR (^)

The bitwise XOR operator produces a 1 if the corresponding bits in two integers are different. For example:

a = 6 # binary: 110
b = 3 # binary: 011
a ^ b # binary: 101 => decimal: 5

Bitwise NOT (~)

The bitwise NOT operator flips all the bits in an integer. For example:

a = 6 # binary: 110
~a # binary: 1010 => decimal: 10

Bitwise Left Shift (<<)

The bitwise left shift operator moves all the bits in an integer to the left by a specified number of positions. A zero is filled in on the right-hand side. For example:

a = 4 # binary: 100
a << 2 # binary: 1000 => decimal: 8

Bitwise Right Shift (>>)

The bitwise right shift operator moves all the bits in an integer to the right by a specified number of positions. The sign bit is filled in on the left-hand side for signed integers, and zeros are filled in for unsigned integers. For example:

a = -5 # binary: 1010 (signed)
a >> 2 # binary: 101 (signed) => decimal: -3

Zero Fill Right Shift (>>>)

The zero fill right shift operator works like the bitwise right shift operator, but it always fills in zeros on the left-hand side for both signed and unsigned integers. For example:

a = -5 # binary: 1010 (signed)
a >>> 2 # binary: 0101 (unsigned) => decimal: 3

Worked Example

Let's write a program that uses bitwise operators to find the number of set bits in an integer. This is also known as the Hamming weight or population count.

def count_bits(n):
count = 0
while n:
n &= (n - 1)
count += 1
return count

numbers = [3, 5, 7, 24, -8]
for number in numbers:
print(f"Number of set bits in {number}: {count_bits(number)}")

Common Mistakes

  1. Misunderstanding the binary representation of integers and how bitwise operators work on them.
  2. Forgetting to handle negative numbers correctly when using right shifts (>> or >>>).
  3. Using bitwise operators in situations where they are not necessary, leading to more complex and less readable code.
  4. Neglecting to use parentheses to clarify the order of operations when combining multiple bitwise operators in a single expression.
  5. Assuming that bitwise operators only work with integers and forgetting about their behavior with floating-point numbers.
  6. Failing to account for the sign extension when using right shifts on signed integers (>>) or zero extension when using right shifts on unsigned integers (>>>).
  7. Overlooking the difference between arithmetic right shift (>>) and logical right shift (>>>) in Python.
  8. Neglecting to consider the endianness of multi-byte integers when working with bitwise operations.
  9. Not understanding the relationship between binary numbers and their decimal equivalents when using bitwise operators.
  10. Failing to test edge cases, such as negative numbers or large integers, when writing programs that use bitwise operators.

Subheadings under Common Mistakes:

  • Handling negative numbers correctly with right shifts
  • Parentheses for clarity in expressions
  • Bitwise operators and floating-point numbers
  • Sign extension vs zero extension
  • Arithmetic right shift vs logical right shift
  • Endianness and multi-byte integers
  • Binary number representation and decimal equivalents
  • Testing edge cases when writing programs that use bitwise operators

Practice Questions

  1. Write a program that uses bitwise OR (|) to find the union of two sets represented as binary numbers.
  2. Write a program that uses bitwise AND (&) to check if a number is even or odd.
  3. Write a program that uses bitwise XOR (^) and left shift (<<) to swap two variables without using a temporary variable.
  4. Write a program that uses bitwise NOT (~) and right shift (>>) to reverse the bits of an integer.
  5. Write a program that uses bitwise AND (&) and left shift (<<) to find the least common multiple of two numbers.
  6. Write a program that uses bitwise operators to check if a number is a power of 2.
  7. Write a program that uses bitwise operators to count the number of unique bits in an integer.
  8. Write a program that uses bitwise operators to find the binary representation of a decimal number.
  9. Write a program that uses bitwise operators to implement a fast exponentiation algorithm (square and multiply method).
  10. Write a program that uses bitwise operators to implement a fast modular exponentiation algorithm for large numbers.

FAQ

  1. Why are bitwise operators important in programming?
  • Bitwise operators allow for efficient manipulation of binary data, which is essential when working with low-level programming tasks or optimizing algorithms.
  1. Can I use bitwise operators with floating-point numbers in Python?
  • No, bitwise operators only work with integers in Python. Floating-point numbers are represented as a sign, exponent, and mantissa, which makes them incompatible with bitwise operations.
  1. What is the difference between >> and >>> in Python?
  • >> performs arithmetic right shift, filling in the leftmost bits with the sign bit for signed integers or zeros for unsigned integers. >>> performs logical right shift, always filling in zeros on the left-hand side.
  1. How can I count the number of set bits in an integer using a single line of code?
  • You can use the built-in Python function bin(n).count('1'), where n is the integer you want to count the set bits for. However, this method may not be as efficient as writing your own implementation when working with large integers.
  1. What are some common mistakes when using bitwise operators in Python?
  • Misunderstanding the binary representation of integers and how bitwise operators work on them, forgetting to handle negative numbers correctly when using right shifts (>> or >>>), using bitwise operators in situations where they are not necessary, neglecting to use parentheses to clarify the order of operations when combining multiple bitwise operators in a single expression, assuming that bitwise operators only work with integers and forgetting about their behavior with floating-point numbers, overlooking the difference between arithmetic right shift (>>) and logical right shift (>>>), neglecting to consider the endianness of multi-byte integers when working with bitwise operations.
  1. What is the relationship between binary numbers and their decimal equivalents when using bitwise operators?
  • Each digit in a binary number corresponds to a power of 2, starting from 0 (2^0) on the rightmost side. The sum of these powers gives the decimal equivalent of the binary number. For example, the binary number 101 is equal to 1*2^2 + 0*2^1 + 1*2^0, which equals 5 in decimal.
  1. Why should I test edge cases when writing programs that use bitwise operators?
  • Testing edge cases ensures that your program works correctly for all possible inputs, including negative numbers, large integers, and special values like zero or one. This helps you catch potential bugs and improve the robustness of your code.
  1. How can I swap two variables using bitwise operators in Python?
  • You can use XOR (^) to exchange the bits between two variables without needing a temporary variable. Here's an example:
a = 5
b = 10
a ^= b
b ^= a
a ^= b
print("a:", a)
print("b:", b)

After running this code, a will be equal to 10 and b will be equal to 5. This is because XOR performs the following operation: x ^ y ^ x = y.

5. Python Bitwise operators | Python | XQA Learn