Master Bitwise Operators in Python: A Complete Guide

Logan Jul 13, 2026

In the realm of computer programming, particularly in Python, bitwise operators play a crucial role in manipulating and analyzing data at the binary level. These operators perform bit-by-bit operations on the operands, which makes them powerful tools for specific tasks such as checking and manipulating flags, calculating efficiency-critical algorithms, and more.

Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples

Bitwise operations involve a deep understanding of binary numbers, which are fundamental to how computers process and store data. Before delving into the bitwise operators, let's quickly refresh our memory on binary numbers: they are base-2 numbers, where digits can only be 0 or 1. Each position in a binary number represents a power of 2, starting from the rightmost position which is 2^0. Now, let's explore the bitwise operators in Python.

Bitwise Operators in Python
Bitwise Operators in Python

Basic Bitwise Operators

The basic bitwise operators are used to perform operations on the binary representation of numbers. They include AND (&), OR (|), XOR (^), NOT (~), and bit shift (<< and >>). We will explore each of these operators and provide examples to illustrate their usage and outcomes.

Python bitwise operators cheat sheet
Python bitwise operators cheat sheet

Let's start with the NOT operator. It is the only unary operator among the bitwise operators and works by inverting all the bits in the number. If a number is represented as a binary string, using the ~ operator will change all 1s to 0s and all 0s to 1s. For example, applying ~ to 9 (1001) would result in -10 (1010).

Bitwise AND (&)

Python Operators Explained for Beginners
Python Operators Explained for Beginners

The bitwise AND operator takes two operands and performs the AND operation on each bit of the two operands. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0. For example, 25 (11001) AND 13 (1101) equals 12 (1100).

Here's how you can use it in Python:

        result = 25 & 13         print(result) # Output: 12

Bitwise OR (|)

Python bitwise operators cheat-sheet
Python bitwise operators cheat-sheet

The bitwise OR operator takes two operands and performs the OR operation on each bit of two operands. If any of the bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0. For example, 25 (11001) OR 13 (1101) equals 31 (11111).

Python example:

        result = 25 | 13         print(result) # Output: 31

Bitwise NOT (~)

Coding For Beginners Python - Learn the Basics - Operators
Coding For Beginners Python - Learn the Basics - Operators

The Bitwise NOT is a unary operator that inverts all the bits of the operands. For example, the binary equivalent of ~10 is 011.

Here's how it works in Python:

Bitwise Operators in Python – Real Python
Bitwise Operators in Python – Real Python
Python Operators Explained 🐍
Python Operators Explained 🐍
Python Operators Tutorial – Chapter 3 Notes
Python Operators Tutorial – Chapter 3 Notes
PYTHON OPERATORS
PYTHON OPERATORS
4 most important Bitwise operator that  every programmer should know
4 most important Bitwise operator that every programmer should know
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
🐍 Python – Bitwise Operators (Complete Beginner Guide)
🐍 Python – Bitwise Operators (Complete Beginner Guide)
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
Math Operators in Python
Math Operators in Python
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
Python Boolean Expressions Explained Simply
Python Boolean Expressions Explained Simply
Python Conditional Statements & Loops
Python Conditional Statements & Loops
a diagram that shows the different types of python tuples and how to use them
a diagram that shows the different types of python tuples and how to use them
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
a man holding up a poster with the words 5 levels of python on it
a man holding up a poster with the words 5 levels of python on it
a poster with the words string method in python
a poster with the words string method in python
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
a diagram with the words python operators on it
a diagram with the words python operators on it
an illustrated diagram shows the different languages used to learn programming and language skills for children
an illustrated diagram shows the different languages used to learn programming and language skills for children
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀

        result = ~10         print(result) # Output: -11 (since -11 in binary is 111111111111111111111111111001)

Bitwise Shifts

Bitwise shifting operators shift the bits of the number to the left (<<) or right (>>). Left shift fills zeros in the rightmost places, and right shift fills zeros in the leftmost places.

Bit shift operators are critical in creating efficient multipliers and dividers. Left shift operation is equivalent to multiplying the number by 2, and the right shift is equivalent to dividing the number by 2.

Bitwise Left Shift (<<)

When the left shift operator (<<) is used, the bits on the left side of the number are filled with zeros, and the number is then moved to the left by the number of places specified. For example, 10 (1010) shifted left by two places results in 40 (101000).

Python example:

        result = 10 << 2         print(result) # Output: 40

Bitwise Right Shift (>>)

The right shift operator (>>) fills zeros to the left of the number and shifts the number to the right by the specified number of places. For example, 10 (1010) shifted right by two places results in 1 (0001).

Python example:

        result = 10 >> 2         print(result) # Output: 1 Bitwise operators are powerful tools in Python, offering functionality that goes beyond simple arithmetic calculations. They open up new avenues for problem-solving and bring efficiency to certain algorithms and processes. To delve deeper, consider exploring the bitwise bit manipulation functions and bitwise XOR (^) operators.