21
NovBitwise Operators in Python: Types and Examples
Bitwise Operator in Python: An Overview
We already discussed the Types of Operators in previous tutorial. In this Python tutorial, we'll explore the syntax, providing a comprehensive overview along with practical examples of bitwise operator.
To further enhance your understanding and application of bitwise operator's concepts, consider enrolling in the best Python Certification Course, to gain knowledge about effective utilization of unary operators for improved problem-solving and time management.
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
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