Bitwise Operators in Python
Python, a versatile programming language, provides various operators to manipulate data. Among these, bitwise operators are often overlooked by novice programmers.
However, mastering bitwise operators can unlock powerful capabilities, especially when dealing with low-level programming, cryptography, and optimization tasks.
In this module, we will explore the fundamentals of bitwise operators in Python, discussing their functionalities and providing practical code examples.
What are Bitwise Operators?
Bitwise operators perform operations on individual bits of binary numbers. They allow you to manipulate data at the bit level, providing fine-grained control over data representation and transformations.
Python supports the following bitwise operators:
- Bitwise AND (&): Sets each bit to 1 if both corresponding bits in the operands are 1.
- Bitwise OR (|): Sets each bit to 1 if either of the corresponding bits in the operands is 1.
- Bitwise XOR (^): Sets each bit to 1 if exactly one of the corresponding bits in the operands is 1.
- Bitwise NOT (~): Inverts all the bits.
- Left Shift (<<): Shifts the bits to the left by the specified number of positions, effectively multiplying the number by 2.
- Right Shift (>>): Shifts the bits to the right by the specified number of positions, effectively dividing the number by 2.
Bitwise AND (&)
The bitwise AND operator compares each bit of the operands and returns 1 if both bits are 1; otherwise, it returns 0. This operator is often used for masking and extracting specific bits from a binary number.
Code Example:
>>> a = 13 # Binary: 1101
>>> b = 7 # Binary: 0111
>>> result = a & b
>>> print(result)
In the example above, we perform a bitwise AND operation between a (13 in decimal or 1101 in binary) and b (7 in decimal or 0111 in binary). The result is 5 (0101 in binary), which represents the common bits between a and b.
Bitwise OR (|)
The bitwise OR operator compares each bit of the operands and returns 1 if either of the corresponding bits is 1; otherwise, it returns 0. It is commonly used for setting specific bits or combining bit flags.
Code Example:
>>> a = 10 # Binary: 1010
>>> b = 6 # Binary: 0110
>>> result = a | b
>>> print(result)
14 (Binary: 1110)
Here, we perform a bitwise OR operation between a (10 in decimal or 1010 in binary) and b (6 in decimal or 0110 in binary). The result is 14 (1110 in binary), which represents the bits that are set in either a or b.
Bitwise XOR (^)
The bitwise XOR operator compares each bit of the operands and returns 1 if exactly one of the corresponding bits is 1; otherwise, it returns 0. It is useful for flipping bits or checking for differences between two binary numbers.
Code Example:
>>> a = 18 # Binary: 10010
>>> b = 9 # Binary: 01001
>>> result = a ^ b
>>> print(result)
27 (Binary: 11011)
In this example, we perform a bitwise XOR operation between a (18 in decimal or 10010 in binary) and b (9 in decimal or 01001 in binary). The result is 27 (11011 in binary), which represents the bits that are different between a and b.
Bitwise NOT (~)
The bitwise NOT operator inverts all the bits of a number. It returns the one's complement of the number, effectively changing each 0 to 1 and vice versa.
Code Example:
>>> a = 42 # Binary: 101010
>>> result = ~a
>>> print(result)
-43 (Binary: -101011)
In this case, we apply the bitwise NOT operator to a (42 in decimal or 101010 in binary). The result is -43 (represented as a negative binary value: -101011). Note that Python uses a two's complement representation for negative numbers.
Left Shift (<<)
The left shift operator shifts the bits of a number to the left by the specified number of positions. It effectively multiplies the number by 2 raised to the power of the shift amount.
Code Example:
>>> a = 7 # Binary: 111
>>> result = a << 2
>>> print(result)
28 (Binary: 11100)
In this example, we left-shift a (7 in decimal or 111 in binary) by 2 positions. The result is 28 (11100 in binary), which represents the original bits shifted two places to the left.
Right Shift (>>)
The right shift operator shifts the bits of a number to the right by the specified number of positions. It effectively divides the number by 2 raised to the power of the shift amount.
Code Example:
>>> a = 50 # Binary: 110010
>>> result = a >> 3
>>> print(result)
6 (Binary: 110)
Explanation: In this case, we right-shift a (50 in decimal or 110010 in binary) by 3 positions. The result is 6 (110 in binary), representing the original bits shifted three places to the right.
Module Takeaways…
Understanding bitwise operators in Python allows you to perform advanced operations at the bit level. This module provided an overview of the main bitwise operators, including AND, OR, XOR, NOT, left shift, and right shift.
By mastering these operators, you gain the ability to manipulate binary data, optimize algorithms, and work with low-level programming tasks more effectively.
Experiment with the provided code examples and explore the power of bitwise operations in your Python projects. See you in the next module of our mastering Python journey: Logical Operators!