Introduction to Bitwise Operators in Python
The Bitwise Operators in Python enable you to bypass the high-level abstractions and operate at the most fundamental binary level of the data. Though commonly used in Web development, the Bitwise Operators are a must in hardware interactions, memory optimizations, and cryptography. You can operate on the binary representation of a number, say 5 (which is equivalent to 0101 in binary form), and perform logical operations at the CPU level in a lightning-fast manner.
Being a master in the “under the hood” mechanics of the language is a prerequisite for writing high-performance, professional code, and that’s what our Python training in Chennai promises to deliver!
1. What are Bitwise Operators?
What are bitwise operators in Python? Bitwise operators in Python are special symbols that are used to perform operations on the binary digits of a binary number. Bitwise operators in Python only operate on integers. When a bitwise operator in Python is applied to two integer numbers, the Python interpreter automatically converts the decimal numbers into binary numbers, operates on the binary digits, and converts the result back into a decimal integer for you.
The Binary Foundation
Before you learn the details of the bitwise operators in Python with examples, you must understand binary.
- Decimal 5 = 22 + 20 = 0101
- Decimal 3 = 21 + 20 = 0011
2. The Core Python Bitwise Operators
There are six primary bitwise operators in the Python language. Let’s discuss the details of the bitwise operators in a summary table before going into the details of the operators.
| Operator | Name | Description | Example (a=5,b=3) |
| & | Bitwise AND | Sets bit to 1 if both bits are 1 | a & b = 1 (0001) |
| | | Bitwise OR | Sets bit to 1 if one of two bits is 1 | a | b = 7 (0111) |
| ^ | Bitwise XOR | Sets bit to 1 if only one of two bits is 1 | a ^ b = 6 (0110) |
| ~ | Bitwise NOT | Inverts all bits (One’s complement) | ~a = -6 |
| << | Left Shift | Shifts bits left by pushing zeros from the right | a << 1 = 10 (1010) |
| >> | Right Shift | Shifts bits right by removing the rightmost bits | a >> 1 = 2 (0010) |
Explore our Python tutorial for Beginners.
3. Deep Dive: The Logical Bitwise Operators
A. Bitwise AND Operator in Python (&)
What is the bitwise and operator in Python? The bitwise and operator in Python compares the binary digits of the two numbers that are used in the operation.
Code Example:
a = 12 # Binary: 1100
b = 10 # Binary: 1010
# ——————–
# Result: 1000 (Decimal 8)
print(a & b) # Output: 8
B. Bitwise OR Operator in Python (|)
The bitwise or operator in Python is similar to the bitwise and operator. It compares each bit of the two integers. If any of the bits is 1, then the result is 1. It is often used to “set” or “turn on” the desired bits in a number.
Code Example:
a = 12 # Binary: 1100
b = 10 # Binary: 1010
# ——————–
# Result: 1110 (Decimal 14)
print(a | b) # Output: 14
C. Bitwise XOR Operator (^)
XOR is an abbreviation for “Exclusive OR.” In other words, the result is 1 only if the two integers being compared have different bits. If they are the same, then the result is 0.
a = 12 # Binary: 1100
b = 10 # Binary: 1010
# ——————–
# Result: 0110 (Decimal 6)
print(a ^ b) # Output: 6
Explore more examples in our Python project ideas.
4. The Shift Operators: Left and Right
Shift operators are similar to the multiplication and division operators, but with faster execution. They shift the integers and divide or multiply by powers of two.
Left Shift (<<)
If an integer is shifted left by n positions, then the integer is multiplied by 2n.
- 5 << 1 → 5 x 2 = 10
- 5 << 2 → 5 x 4 = 20
Right Shift (>>)
If an integer is shifted right by n positions, then the integer is divided by 2n.
- 20 >> 1 → 20 // 2 = 10
- 20 >> 2 → 20 // 4 = 5
5. Bitwise NOT and Two’s Complement (~)
The ~ operator is usually the one that puzzles people the most, especially those new to programming. For Python, it is simply calculated by – (x + 1).
This is due to Python’s implementation of Two’s Complement arithmetic for signed integers. When you reverse all the bits of a signed integer, you change the sign bit, effectively negating it.
Example:
print(~5) # Output: -6
print(~-10) # Output: 9
6. Real World Use Cases for Bitwise Operators
Why should a Python programmer care about bits? There are many real-world uses for these operators. Here are three of them:
A. Bitmasking – Permission Systems
Instead of having six boolean flags in a database for user permissions, you could simply have one integer.
READ = 0b0001
WRITE = 0b0010
EXECUTE = 0b0100
user_permissions = READ | WRITE # User has Read and Write (0011)
# Check if user has EXECUTE permission
if user_permissions & EXECUTE:
print(“Access Granted”)
else:
print(“Access Denied”) # This will run
B. Compression and Graphics
In image processing (e.g., in RGB values), colors are packed into one integer, which is 32 bits long. You use bitwise operations to “extract” Red, Green, and Blue components from this integer.
C. Parity Checks (Error Detection)
XOR is commonly used in checking data corruption in data sent over networks.
Ace your interviews by preparing with our Python interview questions and answers.
7. Performance of Bitwise Operators in Python: Why Use Them?
While Python is not as quick as C++, Python’s bitwise operators take one clock cycle to execute. If you’re designing an algorithm where you multiply by 2 million times, it will be slightly faster to use << 1 than *2, although in Python, interpreter overhead makes this effect negligible. The only real reason to use them is memory efficiency and beauty in logic.
Conclusion
Being proficient in the use of bitwise operators in Python enables you to work with data in its most basic form. You might be building a high-performance permission system, working with low-level network protocols, or just wanting to optimize an algorithm. Bitwise operators are vital to you. Learning the bitwise and operator in Python and the bitwise or operator in Python marks the transition from a person who just uses tools to someone who understands how the machinery works. Explore more courses in our software training institute in Chennai.
