Brand logo of AImore Technologies in white
Brand logo of AImore Technologies in white

Bitwise Operators in Python: Definition, Types, Examples

Bitwise Operators in Python

Bitwise operators in Python perform operations on the individual bits of integers. They work with binary values, where each bit is represented by 0 or 1. These operators are commonly used in bit manipulation, binary calculations, permission flags, and low-level programming.

Python provides six bitwise operators: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Understanding how these operators work helps developers manipulate integer values efficiently and understand how data is processed at the binary level.

What Are Bitwise Operators in Python?

Bitwise operators are symbols that perform logical operations on the binary representations of integers. Instead of comparing entire numbers, they evaluate individual bits according to the rules of each operator.

For example, the integer 5 is represented as 0101 in four-bit binary notation, while 3 is represented as 0011. A bitwise operation compares or shifts these binary digits to produce a new integer.

a = 5
b = 3

print(a & b)  # AND: 1
print(a | b)  # OR: 7
print(a ^ b)  # XOR: 6

Output:

1
7
6

Each operator produces a different result because it applies a different rule to the binary digits.

What Are the Types of Bitwise Operators in Python?

Python has six main bitwise operators. The following table summarizes their symbols, purposes, and results.

Bitwise Operators Summary Table

Operator Symbol Description Example Output
Bitwise AND & Returns 1 when both corresponding bits are 1. 5 & 3 1
Bitwise OR | Returns 1 when at least one corresponding bit is 1. 5 | 3 7
Bitwise XOR ^ Returns 1 when corresponding bits differ. 5 ^ 3 6
Bitwise NOT ~ Inverts the bits of an integer. ~5 -6
Left Shift << Shifts bits to the left. 5 << 2 20
Right Shift >> Shifts bits to the right. 20 >> 2 5

The examples use decimal integers. Their binary representations help explain how each operator produces its result.

Bitwise XOR Operator in Python

The bitwise XOR (exclusive OR) operator in Python uses the caret symbol (^). It compares corresponding bits of two integers and returns 1 when the bits differ and 0 when they match.

XOR is useful for toggling individual bits, comparing binary values, and performing certain parity calculations.

Example

a = 5  # Binary: 0101
b = 3  # Binary: 0011

result = a ^ b
print(result)

Output:

6

The operation works as follows:

Binary of 5 0 1 0 1
Binary of 3 0 0 1 1
XOR result 0 1 1 0

The resulting binary value is 0110, which equals 6 in decimal.

Bitwise AND Operator in Python

The bitwise AND operator (&) compares corresponding bits of two integers. A result bit is 1 only when both input bits are 1. Otherwise, the result bit is 0.

Example

a = 5  # Binary: 0101
b = 3  # Binary: 0011

result = a & b
print(result)

Output:

1

The binary operation 0101 & 0011 produces 0001, which represents the decimal value 1.

AND is often used to check whether a particular bit is set. For example, value & 1 checks the least significant bit of an integer.

Bitwise NOT Operator in Python

The bitwise NOT operator (~) inverts the bits of an integer, changing 0 to 1 and 1 to 0 in its conceptual binary representation.

Python integers behave as though they have an unlimited number of sign-extension bits. As a result, the NOT operator follows the rule ~x == -(x + 1) for integers.

Example

a = 5

result = ~a
print(result)

Output:

-6

The result is -6 because ~5 is equivalent to -(5 + 1). This is an important distinction when working with Python's negative integers.

Bitwise OR Operator in Python

The bitwise OR operator (|) compares corresponding bits of two integers. It returns 1 if either input bit is 1, and returns 0 only when both input bits are 0.

Example

a = 5  # Binary: 0101
b = 3  # Binary: 0011

result = a | b
print(result)

Output:

7

The binary operation 0101 | 0011 produces 0111, which equals 7 in decimal.

OR is useful when setting specific bits in a value without changing other bits that are already set.

Bitwise Left Shift Operator in Python

The bitwise left shift operator (<<) moves the bits of an integer to the left by a specified number of positions. For non-negative integers, shifting left by n positions is equivalent to multiplying by (2^n).

Example

a = 5

result = a << 2
print(result)

Output:

20

The binary representation of 5 is 0101. Shifting it two positions to the left produces 10100, which represents 20 in decimal.

Left shifts are useful for manipulating binary data and working with values stored in specific bit positions.

Bitwise Right Shift Operator in Python

The bitwise right shift operator (>>) moves the bits of an integer to the right by a specified number of positions. For non-negative integers, it is equivalent to integer division by (2^n), with the fractional part discarded.

Example

a = 20

result = a >> 2
print(result)

Output:

5

The binary representation of 20 is 10100. Shifting it two positions to the right produces 00101, which represents 5 in decimal.

For negative integers, Python's right shift behaves like floor division by a power of two, rather than truncating toward zero.

Conclusion

Bitwise operators in Python allow developers to manipulate individual bits of integer values. The AND operator checks matching set bits, OR combines set bits, XOR identifies differing bits, and NOT inverts bits. Left and right shift operators move bits between positions.

Understanding these six operators provides a foundation for binary arithmetic, bit masking, flag management, and other programming tasks involving low-level data manipulation.

Aimore Technologies

Aimore Technologies is the best software training institute in Chennai. We prepare you for the digital future with tailored IT programs in key areas like Web Development, Software Testing, Python, AWS, and Data Science. Guided by skilled trainers, you'll learn not just to understand technology, but to apply it practically.

Top Training Courses
Cloud Computing
Master AWS & Azure with hands-on enterprise-level projects.
Explore Course
Data Science & AI
Master Python, Machine Learning, and Generative AI tools.
Explore Course
Full Stack Development
Build scalable apps with the MERN or MEAN stack architecture.
Explore Course
DevOps Engineering
Learn CI/CD, Kubernetes, and Terraform for high-speed delivery.
Explore Course
Software Testing
Master Selenium and Cypress for automated quality assurance.
Explore Course

Get Your Career Guide

Download our exclusive 2024 salary guide for software professionals in Chennai.
Download PDF

Ready to Launch Your IT Career?

Join 10,000+ students who transformed their careers with Aimore Technologies.

Our Branches Across Chennai

Medavakkam

3/165, Sumangali Second Floor,
Perumbakkam Main Road,
Medavakkam,
Chennai,
Tamil Nadu – 600100

Anna Nagar

Door No. AD–71, "Green Tower",
New Door No. 266,
Plot No. 2326,
4th Ave, AD Block, Anna Nagar,
Chennai, Tamil Nadu – 600040

Porur

39/22, Second Floor,
Mount Poonamallee Rd,
Nethaji Nagar,
Ramakrishna Nagar, Porur,
Chennai, Tamilnadu – 600116

OMR, Karapakkam

1/16/2, Second Floor,
Rajiv Gandhi Road
(Old Mahabalipuram Rd),
IT Corridor, N 198, Karapakkam,
Chennai, Tamil Nadu – 600097

10,000+
Students Trained
4
Branches in Chennai
95%
Placement Rate
10+ Yrs
Industry Experience
Copyright © 2026 Aimore Technologies. All rights reserved.
ISO Certified Institute
mapcrossmenuchevron-downarrow-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram