Bitwise operators are one of the most powerful features of Python. Unlike arithmetic operators that work with decimal numbers, bitwise operators perform operations directly on the binary representation of integers. Since computers store all data in binary (0s and 1s), bitwise operators allow programmers to manipulate data at the lowest level.
Although beginners may not use bitwise operators frequently, they are widely used in system programming, operating systems, networking, embedded systems, cybersecurity, graphics programming, compression algorithms, cryptography, and performance optimization. Understanding these operators helps you understand how computers process information internally.
Python provides six bitwise operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), and >> (Right Shift). These operators compare the bits of two integers and produce a new integer as the result.
For example, suppose you have the numbers 5 and 3. Their binary representations are:
5 = 101
3 = 011
Instead of comparing the decimal values, bitwise operators compare each binary digit (bit) individually to produce the final result.
Bitwise operations may appear difficult at first, but once you understand binary numbers, they become logical and easy to use. Throughout this lesson, you will learn each bitwise operator step by step with binary illustrations, programming examples, business scenarios, and practical applications.
After completing this lesson, you will be able to:
Bitwise operators perform operations directly on the individual bits (0s and 1s) of integer values. Instead of treating numbers as decimal values, Python first converts them into binary, performs the requested operation, and then converts the result back into decimal.
Every integer stored inside a computer is represented using binary digits.
For example:
Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
When you use a bitwise operator, Python compares these binary digits one position at a time.
| Operator | Name | Description |
|---|---|---|
& |
Bitwise AND | Sets a bit to 1 only if both corresponding bits are 1. |
| |
Bitwise OR | Sets a bit to 1 if at least one corresponding bit is 1. |
^ |
Bitwise XOR | Sets a bit to 1 if the corresponding bits are different. |
~ |
Bitwise NOT | Inverts every bit. |
<< |
Left Shift | Shifts bits toward the left. |
>> |
Right Shift | Shifts bits toward the right. |
Bitwise operators are extremely efficient because they work directly on binary data. They are often faster than arithmetic calculations and are commonly used in low-level programming where performance matters.
Common applications include:
Before learning bitwise operators, you must understand binary numbers.
Humans normally use the decimal number system (Base 10), which contains digits from 0 to 9. Computers, however, use the binary number system (Base 2), which contains only two digits:
Each binary digit is called a bit.
Every position in a binary number represents a power of 2.
| Binary Position | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Power of 2 | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
Convert the decimal number 13 into binary.
| Power of 2 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 1 |
Therefore,
13 = 1101
10 = 1010
7 = 0111
12 = 1100
15 = 1111
Understanding binary representation is essential because all bitwise operators work by comparing these binary digits.
The Bitwise AND operator compares each pair of corresponding bits. It returns 1 only when both bits are 1. If either bit is 0, the result is 0.
number1 & number2
| Bit A | Bit B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
a = 5
b = 3
print(a & b)
Output:
1
Decimal Values
5 = 101
3 = 011
Bitwise AND
101
& 011
-----
001
Binary 001 = Decimal 1
x = 12
y = 10
print(x & y)
Output:
8
Binary calculation:
12 = 1100
10 = 1010
1100
1010
----
1000
Binary 1000 = Decimal 8
A company stores user permissions as binary flags. A Bitwise AND operation can determine whether a user has a specific permission.
READ = 4 # 100
WRITE = 2 # 010
user_permission = 6 # 110
print(user_permission & READ)
Output:
4
Since the result is not zero, the user has READ permission.
Suppose a dataset uses binary flags to indicate whether a customer is eligible for a promotion. A Bitwise AND operation can quickly determine eligibility.
PREMIUM = 1
customer_flag = 5
print(customer_flag & PREMIUM)
Output:
1
The result indicates that the customer satisfies the required binary flag.
In this section, you learned the fundamentals of Python bitwise operators and understood why binary numbers are essential for performing bitwise operations. You explored how computers represent decimal values in binary form and studied the Bitwise AND (&) operator using truth tables, binary calculations, programming examples, business scenarios, and Data Analytics applications. In the next section, you will learn the Bitwise OR (|), Bitwise XOR (^), and Bitwise NOT (~) operators with detailed binary examples.
The Bitwise OR operator compares each pair of corresponding bits. It returns 1 if at least one of the bits is 1. It returns 0 only when both bits are 0.
number1 | number2
| Bit A | Bit B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
a = 5
b = 3
print(a | b)
Output:
7
5 = 101
3 = 011
101
|011
----
111
Binary 111 = Decimal 7
x = 12
y = 10
print(x | y)
Output:
14
12 = 1100
10 = 1010
1100
1010
----
1110
Binary 1110 = Decimal 14
A company stores multiple user permissions using binary flags. The Bitwise OR operator can combine multiple permissions into a single value.
READ = 4
WRITE = 2
permissions = READ | WRITE
print(permissions)
Output:
6
The resulting value represents both READ and WRITE permissions.
A data processing system combines multiple feature flags before running an analysis.
FEATURE_A = 1
FEATURE_B = 2
enabled = FEATURE_A | FEATURE_B
print(enabled)
Output:
3
The Bitwise XOR (Exclusive OR) operator returns 1 only when the corresponding bits are different. If both bits are the same, the result is 0.
number1 ^ number2
| Bit A | Bit B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
a = 5
b = 3
print(a ^ b)
Output:
6
5 = 101
3 = 011
101
^011
----
110
Binary 110 = Decimal 6
x = 12
y = 10
print(x ^ y)
Output:
6
12 = 1100
10 = 1010
1100
1010
----
0110
Binary 0110 = Decimal 6
A security application uses the XOR operator as part of a simple encryption process.
data = 25
key = 12
encrypted = data ^ key
print(encrypted)
Output:
21
Applying XOR with the same key again restores the original value.
original = encrypted ^ key
print(original)
Output:
25
During data comparison, XOR can help identify differences between binary feature flags.
old_flag = 5
new_flag = 7
print(old_flag ^ new_flag)
Output:
2
This indicates that one binary flag has changed.
The Bitwise NOT operator inverts every bit of a number. All 1s become 0s, and all 0s become 1s. It is a unary operator, meaning it operates on only one operand.
~number
x = 5
print(~x)
Output:
-6
Python represents negative integers using two’s complement. The result of the Bitwise NOT operation follows the mathematical relationship:
~x = -(x + 1)
For example:
x = 5
~5
= -(5 + 1)
= -6
x = 10
print(~x)
Output:
-11
Calculation:
~10
= -(10 + 1)
= -11
A system administrator uses Bitwise NOT to invert permission masks while configuring user access settings.
mask = 7
print(~mask)
Output:
-8
Some low-level data processing applications invert binary masks before filtering large datasets.
filter_mask = 15
print(~filter_mask)
Output:
-16
| Operator | Symbol | Purpose | Example |
|---|---|---|---|
| Bitwise AND | & |
Returns 1 only if both bits are 1. | 5 & 3 = 1 |
| Bitwise OR | | |
Returns 1 if at least one bit is 1. | 5 | 3 = 7 |
| Bitwise XOR | ^ |
Returns 1 when bits differ. | 5 ^ 3 = 6 |
| Bitwise NOT | ~ |
Inverts all bits. | ~5 = -6 |
In this section, you learned the Bitwise OR (|), Bitwise XOR (^), and Bitwise NOT (~) operators. You explored their truth tables, binary calculations, Python programming examples, business applications, and Data Analytics use cases. You also learned why the Bitwise NOT operator returns negative values in Python using the two’s complement representation. In the next section, you will study the Left Shift (<<) and Right Shift (>>) operators, along with additional real-world examples, common mistakes, and best practices.
The Left Shift operator shifts all bits of a number to the left by a specified number of positions. Each shift to the left adds a 0 on the right side.
For positive integers, shifting a number left by one position is equivalent to multiplying it by 2. Shifting left by two positions multiplies it by 4, and so on.
number << positions
x = 5
print(x << 1)
Output:
10
5 = 0101
Shift Left by 1
0101
↓
1010
Binary 1010 = Decimal 10
x = 5
print(x << 2)
Output:
20
5 = 0101
Shift Left by 2
0101
↓↓
10100
Binary 10100 = Decimal 20
A graphics application increases image scaling factors by using bit shifting instead of multiplication to improve performance.
scale = 8
print(scale << 1)
Output:
16
A high-performance data processing application multiplies integer counters using left shift operations during large-scale computations.
records = 100
print(records << 1)
Output:
200
The Right Shift operator shifts all bits of a number toward the right by the specified number of positions.
For positive integers, shifting right by one position is approximately equivalent to dividing by 2 and discarding any remainder.
number >> positions
x = 20
print(x >> 1)
Output:
10
20 = 10100
Shift Right by 1
10100
↓
01010
Binary 01010 = Decimal 10
x = 20
print(x >> 2)
Output:
5
20 = 10100
Shift Right by 2
10100
↓↓
00101
Binary 00101 = Decimal 5
An embedded device divides sensor values efficiently using right shift operations instead of arithmetic division.
sensor = 64
print(sensor >> 2)
Output:
16
A real-time analytics system reduces integer counters while processing compressed binary data.
counter = 256
print(counter >> 3)
Output:
32
| Operator | Name | Description | Example |
|---|---|---|---|
& |
Bitwise AND | 1 only if both bits are 1 | 5 & 3 = 1 |
| |
Bitwise OR | 1 if either bit is 1 | 5 | 3 = 7 |
^ |
Bitwise XOR | 1 if bits are different | 5 ^ 3 = 6 |
~ |
Bitwise NOT | Inverts all bits | ~5 = -6 |
<< |
Left Shift | Shift bits left | 5 << 1 = 10 |
>> |
Right Shift | Shift bits right | 20 >> 2 = 5 |
Operating systems often use individual bits to represent user permissions such as Read, Write, and Execute.
READ = 4
WRITE = 2
EXECUTE = 1
permission = READ | WRITE
print(permission)
Output:
6
Image editing software manipulates the Red, Green, Blue (RGB), and Alpha channels using bitwise operations for faster pixel processing.
IP addresses, subnet masks, and network packets use bitwise operations to calculate network addresses and determine routing information.
Compression algorithms such as ZIP and PNG use bitwise operations internally to encode and decode binary data efficiently.
Many encryption and hashing algorithms perform millions of bitwise operations every second to secure digital communication.
PREMIUM = 4
ACTIVE = 2
VERIFIED = 1
customer = PREMIUM | VERIFIED
print(customer)
Output:
5
READ = 4
permission = 6
print(permission & READ)
Output:
4
Binary masks are commonly used during preprocessing to isolate particular bits from encoded values.
value = 29
mask = 15
print(value & mask)
Output:
13
Large datasets sometimes store multiple status flags inside a single integer using bitwise operations to reduce storage requirements.
High-performance analytics applications occasionally replace multiplication and division by powers of two with left and right shift operations.
and, or) with bitwise operators (&, |).READ, WRITE, and EXECUTE when working with bit masks.In this section, you learned how the Left Shift (<<) and Right Shift (>>) operators move bits to perform efficient multiplication and division by powers of two. You explored practical business applications, Data Analytics use cases, common beginner mistakes, and best practices for writing readable and efficient bitwise code. In the final section, you will review the complete lesson through a summary, key takeaways, FAQs, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Operator Precedence.
In this lesson, you learned how Python bitwise operators work by performing operations directly on the binary representation of integers. Unlike arithmetic operators, bitwise operators manipulate individual bits (0s and 1s), making them useful for low-level programming, system optimization, networking, cryptography, embedded systems, and performance-critical applications.
You first learned how computers represent decimal numbers in binary form and why understanding binary numbers is essential before using bitwise operators. You then explored all six Python bitwise operators:
& (Bitwise AND)| (Bitwise OR)^ (Bitwise XOR)~ (Bitwise NOT)<< (Left Shift)>> (Right Shift)Throughout the lesson, you learned how each operator works using truth tables, binary calculations, Python code examples, business scenarios, and Data Analytics applications. You also discovered that left shift operations can efficiently multiply numbers by powers of two, while right shift operations divide positive integers by powers of two.
Although bitwise operators are not used in everyday Python programming, they are essential for writing efficient system software, processing binary data, managing permissions, optimizing applications, and working with hardware-level programming.
& returns 1 only when both bits are 1.| returns 1 if at least one bit is 1.^ returns 1 when two bits are different.~ inverts every bit and follows the formula ~x = -(x + 1).<< shifts bits left and multiplies positive integers by powers of two.>> shifts bits right and divides positive integers by powers of two.Bitwise operators perform operations directly on the binary representation of integers.
Python provides six bitwise operators: &, |, ^, ~, <<, and >>.
It returns 1 only when both corresponding bits are 1.
It returns 1 when the corresponding bits are different.
~5 return -6?Python uses two’s complement representation for signed integers, so ~x = -(x + 1).
and and &?and is a logical operator that works with Boolean expressions, whereas & is a bitwise operator that works on the binary representation of integers.
No. Bitwise operators work only with integer values.
They are used in operating systems, networking, embedded systems, cryptography, graphics programming, compression algorithms, and hardware programming.
They provide an efficient way to multiply or divide positive integers by powers of two.
Yes. They are used for binary feature flags, data masking, status codes, permissions, and performance optimization in specialized applications.
bin() function and perform bitwise operations.* with left shift operations.Create a Python program that manages user permissions using bitwise operators.
Your program should:
READ, WRITE, and EXECUTE using binary values.bin() function.This project demonstrates one of the most common real-world applications of bitwise operators in operating systems, security software, and enterprise applications.
Congratulations! You have successfully completed the lesson on Python Bitwise Operators. You now understand how binary numbers work, how each bitwise operator performs calculations, and where these operators are used in real-world software development.
In the next lesson, you will learn Python Operator Precedence. You will understand how Python decides the order in which operators are evaluated, explore precedence rules with practical examples, learn the role of parentheses, and write expressions that are both correct and easy to read.