Python Bitwise Operators (&, |, ^, ~, <<, >>)
What Are Bitwise Operators?
Python Bitwise operators perform operations on the binary representations of integers. Instead of working with the decimal numbers you see (like 10 or 5), these operators work on the underlying bits (zeros and ones).
Prerequisite: Understanding Binary
Computers store integers as sequences of bits. To understand these operators, you must visualize the numbers in binary. To learn better, understand binary to decimal conversion.
Binary: 1010 (8 + 0 + 2 + 0)
Decimal: 4
Binary: 0100 (0 + 4 + 0 + 0)
You will rarely use bitwise operators for general logic (like normal if statements). We use them primarily for working with flags, permissions systems, binary protocols, and mathematical optimizations.
1. Bitwise AND (&)
The AND operator compares two bits. The result is 1 only if both corresponding bits are 1. Otherwise, the result is 0.
Logic:
- 1 & 1 = 1
- 1 & 0 = 0
- 0 & 1 = 0
- 0 & 0 = 0
Syntax and Examples
a = 10
b = 4
result = a & b
print(f"Result of 10 & 4 is: {result}")
Your output will appear here...
2. Bitwise OR (|)
The OR operator compares two bits. The result is 1 if at least one of the corresponding bits is 1.
Logic:
- 1 | 1 = 1
- 1 | 0 = 1
- 0 | 1 = 1
- 0 | 0 = 0
Syntax and Examples
a = 10
b = 4
result = a | b
print(f"Result of 10 | 4 is: {result}")
Your output will appear here...
3. Bitwise XOR (^)
XOR stands for "Exclusive OR". The result is 1 only if the corresponding bits are different. If they are the same (both 0 or both 1), the result is 0.
Logic:
- 1 ^ 1 = 0
- 1 ^ 0 = 1
- 0 ^ 1 = 1
- 0 ^ 0 = 0
Syntax and Examples
a = 10
b = 4
result = a ^ b
print(f"Result of 10 ^ 4 is: {result}")
Your output will appear here...
4. Bitwise NOT (~)
The NOT operator is a unary operator, meaning it works on a single number. It inverts all the bits: 0 becomes 1, and 1 becomes 0.
Important Note on Output: Python uses "Two's Complement" to represent signed integers. In Two's Complement, the result of ~x is mathematically equal to -x - 1.
Syntax and Examples
a = 10
result = ~a
print(f"Result of ~10 is: {result}")
Your output will appear here...
5. Bitwise Left Shift (<<)
This operator shifts the bits of the first operand to the left by the number of places specified by the second operand. New bits on the right are filled with zeros.
Effect: Shifting left by 1 is equivalent to multiplying the number by 2.
Syntax and Examples
a = 10
shift_by = 2
result = a << shift_by
print(f"Result of 10 << 2 is: {result}")
Your output will appear here...
6. Bitwise Right Shift (>>)
This operator shifts the bits of the first operand to the right by the number of places specified by the second operand. Bits that are shifted off the end are discarded.
Effect: Shifting right by 1 is equivalent to performing integer division by 2.
Syntax and Examples
a = 10
shift_by = 1
result = a >> shift_by
print(f"Result of 10 >> 1 is: {result}")
Your output will appear here...
When to Use Logical vs. Bitwise Operators
Use bitwise operators to manipulate individual bits in integers, flags, or low-level data. They work for tasks like masking, shifting, and bit-level optimization. Use logical operators to combine boolean conditions in control flow (e.g., if, while statements) or simplify logical expressions.
Summary Table
| Operator | Name | Description | Example |
|---|---|---|---|
& |
AND | Sets bit to 1 if both bits are 1 | 5 & 3 |
| |
OR | Sets bit to 1 if either bit is 1 | 5 | 3 |
^ |
XOR | Sets bit to 1 if bits are different | 5 ^ 3 |
~ |
NOT | Inverts the bits | ~5 |
<< |
Left Shift | Shifts bits left (multiplies by 2) | 5 << 1 |
>> |
Right Shift | Shifts bits right (divides by 2) | 5 >> 1 |
Lesson Completed
You have successfully learned about the six Bitwise Operators in Python, how they manipulate binary data, and their mathematical effects.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now