20
DecBitwise Operators in Python: Types and Examples
Bitwise Operator in Python
We already discussed the Types of Operators in the previous tutorial. Now, Let’s talk about bitwise operators in Python. These are special operators that work directly at the bit level of numbers. Imagine breaking numbers into their binary form (1s and 0s) and performing operations like AND, OR, XOR, shifts, and more. They’re super handy when you want to optimize low-level operations, like working with flags, cryptography, or even graphics programming.
Don’t worry if it sounds complex, it’s easier than it looks, and in this Python tutorial, we'll explore bitwise operators in Python in detail, providing a comprehensive overview along with practical examples of bitwise operators.
What is a Bitwise Operator in Python?
Bitwise operators in Python are used to perform bit-level operations on integers. These operators work on the binary representation of data, manipulating individual bits. Understanding bitwise operators is crucial when dealing with low-level programming or optimizing certain algorithms.
Syntax:
The syntax for bitwise operators in Python is concise and easy to understand. The primary bitwise operators include:
- & (Bitwise AND)
- | (Bitwise OR)
- ~ (Bitwise NOT)
- ^ (Bitwise XOR)
- << (Bitwise Left Shift)
- >> (Bitwise Right Shift)
Types of Bitwise Operators
Operator | Name | Description | Example | Result |
& | Bitwise AND | Performs AND operation on corresponding bits of two numbers. | 5 & 3 | 1 |
| | Bitwise OR | Performs OR operation on corresponding bits of two numbers. | 5 | 3 | 7 |
^ | Bitwise XOR | Performs XOR operation on corresponding bits of two numbers (1 if bits are different). | 5 ^ 3 | 6 |
~ | Bitwise NOT | Flips all bits of the number (one's complement). | ~5 | -6 |
<< | Left Shift | Shifts bits to the left by the specified number of positions (fills with 0s on the right). | 5 << 2 | 20 |
>> | Right Shift | Shifts bits to the right by the specified number of positions (fills with sign bit or 0s on the left). | 5 >> 2 | 1 |
1. Bitwise AND
The bitwise AND operator compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.
result_and = 5 & 3 # Binary: 0101 & 0011 = 0001
print("Bitwise AND: 5 & 3 =", result_and)
Output
Bitwise AND: 5 & 3 = 1
Read More - 50 Python Interview Questions and Answers
2. Bitwise OR
The bitwise OR operator compares each bit of the first operand to the corresponding bit of the second operand. If at least one bit is 1, the result is 1; otherwise, it is 0.
result_or = 5 | 3 # Binary: 0101 | 0011 = 0111
print("Bitwise OR: 5 | 3 =", result_or)
Output
Bitwise OR: 5 | 3 = 7
3. Bitwise NOT
The bitwise NOT operator inverts each bit of its operand. It changes 1s to 0s and vice versa, effectively flipping the bits.
result_not = ~5 # Binary: ~0101 = 1010 (Two's complement)
print("Bitwise NOT: ~5 =", result_not)
Output
Bitwise NOT: ~5 = -6
4. Bitwise XOR
The bitwise XOR operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the result is 1; if they are the same, the result is 0.
result_xor = 5 ^ 3 # Binary: 0101 ^ 0011 = 0110
print("Bitwise XOR: 5 ^ 3 =", result_xor)
Output
Bitwise XOR: 5 ^ 3 = 6
Read More - Python Developer Salary in India
5. Bitwise Shift Operators
- Bitwise Left Shift
The bitwise left shift operator shifts the bits of the first operand to the left by a specified number of positions. Zeros are added to the vacant positions on the right.
result_left_shift = 5 << 1 # Binary: 0101 << 1 = 1010
print("Bitwise Left Shift: 5 << 1 =", result_left_shift)
Output
Bitwise Left Shift: 5 << 1 = 10
- Bitwise Right Shift
The bitwise right shift operator shifts the bits of the first operand to the right by a specified number of positions. The vacant positions on the left are filled based on the sign bit for signed integers and with zeros for unsigned integers.
result_right_shift = 5 >> 1 # Binary: 0101 >> 1 = 0010
print("Bitwise Right Shift: 5 >> 1 =", result_right_shift)
Output
Bitwise Right Shift: 5 >> 1 = 2
Examples of Bitwise Operators in Python Compiler
# Bitwise AND
result_and = 5 & 3 # Result: 1
# Bitwise OR
result_or = 5 | 3 # Result: 7
# Bitwise NOT
result_not = ~5 # Result: -6
# Bitwise XOR
result_xor = 5 ^ 3 # Result: 6
# Bitwise Left Shift
result_left_shift = 5 << 1 # Result: 10
# Bitwise Right Shift
result_right_shift = 5 >> 1 # Result: 2
# Displaying Results
print("Bitwise AND: 5 & 3 =", result_and)
print("Bitwise OR: 5 | 3 =", result_or)
print("Bitwise NOT: ~5 =", result_not)
print("Bitwise XOR: 5 ^ 3 =", result_xor)
print("Bitwise Left Shift: 5 << 1 =", result_left_shift)
print("Bitwise Right Shift: 5 >> 1 =", result_right_shift)
Output
Bitwise AND: 5 & 3 = 1
Bitwise OR: 5 | 3 = 7
Bitwise NOT: ~5 = -6
Bitwise XOR: 5 ^ 3 = 6
Bitwise Left Shift: 5 << 1 = 10
Bitwise Right Shift: 5 >> 1 = 2
Bitwise Operator Overloading in Python
Overview of Special Methods for Overloading:Operator | Special Method | Description |
& | __and__(self, other) | Defines behavior for the bitwise AND operator. |
| | __or__(self, other) | Defines behavior for the bitwise OR operator. |
^ | __xor__(self, other) | Defines behavior for the bitwise XOR operator. |
~ | __invert__(self) | Defines behavior for the bitwise NOT operator. |
<< | __lshift__(self, other) | Defines behavior for the left shift operator. |
>> | __rshift__(self, other) | Defines behavior for the right shift operator. |
Example
class BitwiseExample:
def __init__(self, value):
self.value = value
def __and__(self, other):
return BitwiseExample(self.value & other.value)
def __or__(self, other):
return BitwiseExample(self.value | other.value)
def __xor__(self, other):
return BitwiseExample(self.value ^ other.value)
def __invert__(self):
return BitwiseExample(~self.value)
def __lshift__(self, other):
return BitwiseExample(self.value << other)
def __rshift__(self, other):
return BitwiseExample(self.value >> other)
def __str__(self):
return str(self.value)
# Usage Example
a = BitwiseExample(5) # Binary: 0101
b = BitwiseExample(3) # Binary: 0011
print("a & b =", a & b) # Bitwise AND
print("a | b =", a | b) # Bitwise OR
print("a ^ b =", a ^ b) # Bitwise XOR
print("~a =", ~a) # Bitwise NOT
print("a << 2 =", a << 2) # Left Shift
print("a >> 1 =", a >> 1) # Right Shift
Output
a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 2 = 20
a >> 1 = 2