Every integer stored in a computer is represented using binary digits (0s and 1s). Bitwise operators work directly on these binary values, making them useful for low-level programming, data compression, cryptography, networking, image processing, and embedded systems.
Although beginners may not use bitwise operators frequently, understanding them helps in learning how computers process data internally. In this article, we will explore the different types of bitwise operators, understand how they work with binary numbers, and solve practical examples.
Table of Contents
What are Bitwise Operators?
Bitwise operators are operators that perform operations on the binary form of integers. Before performing an operation, Python converts the decimal numbers into binary numbers. After the operation is completed, the binary result is converted back into a decimal number.Example:
a = 5 b = 3 print(a & b)Output:
1Explanation:
- First, Python converts both numbers into binary.
- 5 = 0101
- 3 = 0011
- Now the AND operation is performed.
- 0101
- 0011
- 0001
- The binary number "0001" is equal to the decimal number 1.
Why are Bitwise Operators Important?
Bitwise operators are useful because they allow programmers to work directly with binary data. They are commonly used in system programming and applications where performance is important.Some advantages of bitwise operators are:
- Faster Computation: Bitwise operations are generally faster than many arithmetic operations because they work directly with binary values.
- Efficient Memory Usage: They help programmers manipulate data using fewer memory resources.
- Used in Low-Level Programming: Bitwise operators are widely used in operating systems, device drivers, and embedded systems.
- Useful in Data Manipulation: They help set, clear, toggle, and check individual bits in binary numbers.
- Important in Networking and Security: Bitwise operations are commonly used in networking protocols, encryption algorithms, and data compression techniques.
Types of Bitwise Operators
Python provides six bitwise operators.1. Bitwise AND ("&"): The Bitwise AND operator compares each bit of two numbers. It returns 1 only if both bits are 1. Otherwise, it returns 0
Truth Table for Bitwise AND ("&"):
| A | B | A & B |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example:
# Python program to implement Bitwise AND operation a = 5 b = 3 print(a & b)Output:
1Binary Calculation:
Explanation: If one bit is 0, the result becomes 0. Therefore, the binary result "0001" equals the decimal number 1.5 = 0101
3 = 0011
Calculating Binary in comparision to"&"
0101
& 0011
0001
2. Bitwise OR ("|"): The Bitwise OR operator returns 1 if at least one of the bits is 1.
Truth Table for Bitwise OR("|"):
| A | B | A | B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Example:
# Python program to implement Bitwise OR operation a = 5 b = 3 print(a | b)Output:
7Binary Calculation:
Explanation: If one bit is 1, the result becomes 1. Therefore, the binary result "0111" equals the decimal number 7.5 = 0101
3 = 0011
Calculating Binary in comparision to"|"
0101
| 0011
0111
3. Bitwise XOR ("^"): The Bitwise XOR operator returns 1 only when the two bits are different. If both bits are the same, it returns 0.
Truth Table for Bitwise XOR("^"):
| A | B | A ^ B |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example:
# Python program to implement Bitwise XOR operation a = 5 b = 3 print(a ^ b)Output:
6Binary Calculation:
Explanation:5 = 0101
3 = 0011
Calculating Binary in comparision to"^"
0101
^ 0011
0110
- The XOR operator checks each pair of bits.
- Same bits = 0 , Different bits = 1
- Therefore, the binary result "0110" equals the decimal number 6.
4. Bitwise NOT ("~"): The Bitwise NOT operator is a unary operator, which means it works on only one operand. It inverts all the bits of a number, changing every 1 to 0 and every 0 to 1.
Truth Table for Bitwise NOT ("~")
| A | -A |
| 0 | 1 |
| 1 | 0 |
Example:
# Python program to implement Bitwise NOT operation a = 5 print(~a)
Output:
-6
Explanation:
- The binary representation of "5" is: 0101
- After applying the NOT operator, all bits are reversed = 1010
- Python uses 2's complement representation for negative numbers, so the result becomes "-6".
5. Bitwise Left Shift ("<<"): The Left Shift operator shifts all the bits of a number to the left by the given number of positions. Every left shift multiplies the number by 2.
Example:
# Python program to implement Bitwise Left Shift operation a = 5 print(a<<1)
Output:
10
Binary Calculation:
- 5 = 0101
- Shift Left by 1
- 0101 = 1010
- 0's will be added on the right side
Explanation:
- Shifting left by one position is also equal to multiplying the number by 2.
- 5 × 2 = 10
6. Bitwise Right Shift (">>"): The Right Shift operator shifts all the bits of a number to the right by the given number of positions. Every right shift divides the number by 2 (ignoring the remainder).
Example:
# Python program to implement Bitwise Light Shift operation a = 8 print(a>>1)
Output:
4
Binary Calculation:
- 8 = 1000
- Shift Right by 1
- 1000 → 0100
- 0's will be added on the left side
Explanation:
- Shifting right by one position is equivalent to dividing the number by 2.
- 8 ÷ 2 = 4
Complete Program Using Bitwise Operators
# Python program to implement all bitwise operations # Assigning values a = 5 b = 3 # Bitwise AND print(a & b) # Bitwise OR print(a | b) # Bitwise XOR print(a ^ b) # Bitwise NOT print(~a) # Bitwise Left Shift print(a<<1) # Bitwise Right Shift print(a>>1)
Output:
AND: 1
OR: 7
XOR: 6
NOT: -6
Left Shift: 10
Right Shift: 2
Explanation:
- a = 5 and b = 3: Create two integer variables.
- print("AND:", a & b): Performs the Bitwise AND operation and returns "1".
- print("OR:", a | b): Performs the Bitwise OR operation and returns "7".
- print("XOR:", a ^ b): Performs the Bitwise XOR operation and returns "6".
- print("NOT:", ~a): Reverses all the bits of "5" and returns "-6".
- print("Left Shift:", a<<1): Shifts all bits one position to the left, multiplying the number by "2".
- print("Right Shift:", a>>1): Shifts all bits one position to the right, dividing the number by "2".
Common Beginner Mistakes
1. Confusing Logical Operators with Bitwise Operators: Bitwise operators work on binary values, whereas logical operators work on Boolean values.
Wrong:
print(True & False)
Correct:
print(True and False)
2. Expecting "^" to Calculate Power: Many beginners think "^" is the exponent operator.
Wrong:
print(2 ^ 3)
Correct:
print(2 ** 3)
3. Forgetting That "~" Returns a Negative Number: Beginners often expect the NOT operator to return a positive number.
Wrong Expectation:
print(~5)
# Expected: 6
Actual Output:
print(~5)
# Output: -6
4. Using Bitwise Operators with Floating-Point Numbers: Bitwise operators work only with integers.
Wrong:
print(5.5 & 3)
Correct:
print(5 & 3)
5. Forgetting the Effect of Shift Operators: Left shift multiplies the number by 2, while right shift divides the number by 2 for each shift position.
Wrong:
print(4 2) # Expected: 8
Correct:
print(4 2) # Output: 16
Conclusion
Bitwise operators allow Python programs to perform operations directly on the binary representation of integers. These operators are useful in system programming, networking, cryptography, image processing, and embedded systems.
Although beginners may not use bitwise operators frequently, understanding how they work provides valuable insight into how computers process data. Learning these operators also builds a strong foundation for advanced programming concepts.
Frequently Asked Questions
1. What are bitwise operators in Python?
Bitwise operators are used to perform operations directly on the binary representation of integers.
2. How many bitwise operators are available in Python?
Python provides six bitwise operators: "&", "|", "^", "~", "", and "".
3. What is the difference between logical and bitwise operators?
Logical operators work with Boolean values ("True" and "False"), while bitwise operators work with the binary bits of integers.
4. Which operator is used for left shift?
The "" operator is used for left shift.
5. Why does "~5" return "-6"?
Python uses 2's complement representation for integers. Therefore, applying the Bitwise NOT operator to "5" results in "-6".
6. Can bitwise operators be used with floating-point numbers?
No. Bitwise operators can only be used with integer data types.
0 Comments