Master Bitwise Operators in Python with Easy Examples and Code Snippets

Logan Jul 13, 2026

Bitwise operators are extremely useful in Python, especially when dealing with bits, bytes, and memory-efficient data manipulation. They enable operations on binary representations, offering an efficient way to perform tasks like bit manipulation, data masking, and flag checking.

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

In this article, we delve into Python's bitwise operators, their usage, and key examples. By understanding and effectively utilizing these operators, you can enhance your coding skills and improve performance.

Python bitwise operators cheat sheet
Python bitwise operators cheat sheet

Bitwise Operators Overview

Pyton provides six bitwise operators: bitwise AND (&), OR (|), XOR(^), Not(~), Left Shift (<<), and Right Shift (>>). These operators take two operands and return the result of their bit-by-bit operations.

Python Operators Explained for Beginners
Python Operators Explained for Beginners

Each operator manipulates the binary representation of integers, thus demanding proficiency in understanding binary numbers for effective use. Here's a concise overview:

  • Bitwise AND (&): Returns 1 only if both bits are 1. Else, returns 0.
  • Bitwise OR (|): Returns 1 if any of the two bits is 1.
  • Bitwise XOR (^): Returns 1 if the number of 1's in the corresponding bits of the operands is odd.
  • Bitwise NOT (~): Inverts all the bits.
  • Bitwise Left Shift (<<): Shifts the bits of the left operand to the left by the number of places specified by the right operand.
  • Bitwise Right Shift (>>): shifts the bits of the right operand to the right by the number of places specified by the left operand.
Types of Operators in Python
Types of Operators in Python

Bitwise Operators in Action - Example

Let's consider two integer variables, a = 60 (binary 0011 1100) and b = 13 (binary 0000 1101). We'll use them to demonstrate the six bitwise operators:

Bitwise AND (&)

Python Operators Tutorial – Chapter 3 Notes
Python Operators Tutorial – Chapter 3 Notes

The binary representation of the result of a & b (60 & 13) is 0000 1100, which is 12 in decimal.

In Python:

a = 60
b = 13
print(a & b) # Outputs: 12

Bitwise OR (|)

Bitwise Operators in Python
Bitwise Operators in Python

The binary representation of the result of a | b (60 | 13) is 0011 1101, which is 61 in decimal.

In Python:

Coding For Beginners Python - Learn the Basics - Operators
Coding For Beginners Python - Learn the Basics - Operators
Python bitwise operators cheat-sheet
Python bitwise operators cheat-sheet
Understanding bitwise operators in Python
Understanding bitwise operators in Python
PYTHON OPERATORS
PYTHON OPERATORS
Bitwise Operators in Python – Real Python
Bitwise Operators in Python – Real Python
Python Operators Explained 🐍
Python Operators Explained 🐍
Python Operators 😎✨
Python Operators 😎✨
Mastering Bitwise Operators in Python: A Complete Guide
Mastering Bitwise Operators in Python: A Complete Guide
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
Python - Bitwise Operators
a diagram with the words python operators on it
a diagram with the words python operators on it
Math Operators in Python
Math Operators in Python
Bitwise Operator
Bitwise Operator
Python Operators: The Complete Guide for Beginners
Python Operators: The Complete Guide for Beginners
Python - Basic Operators: Types of Operator
Python - Basic Operators: Types of Operator
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
comparison between two types of operators in python infographical poster on blackboard background
comparison between two types of operators in python infographical poster on blackboard background
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Coding For Beginners Python - Learn the Basics - Operators
Coding For Beginners Python - Learn the Basics - Operators
A complete guide for Bitwise operators in numpy
A complete guide for Bitwise operators in numpy

print(a | b) # Outputs: 61

Bitwise XOR (^)

The binary representation of the result of a ^ b (60 ^ 13) is 0011 0001, which is 49 in decimal. Note that each pair of corresponding bits does not share the same value; the last pair's different values return as 1.

In Python:

print(a ^ b) # Outputs: 49

Bitwise NOT (~)

The binary representation of the result of ~a (~60) is 1100 0011, which equals -61 in decimal. This operation inverts the bits: 1s become 0s and vice versa.

In Python:

print(~a) # Outputs: -61

Bitwise Left Shift (<<)

The binary representation of the result of a << 2 (60 << 2) is 1111 0000, which is 240 in decimal. Bits of 'a' are moved two places to the left.

In Python:

print(a << 2) # Outputs: 240

Bitwise Right Shift (>>)

The binary representation of the result of a >> 2 (60 >> 2) is 0011 1100, which is 60 in decimal. Bits of 'a' are moved two places to the right.

In Python:

print(a >> 2) # Outputs: 15

Understanding and properly employing Python's bitwise operators can significantly improve the performance and efficiency of your Python code, especially in scenarios involving memory-constrained systems and bit-level manipulators. Happy coding!