Bitwise operators in Python are used to perform operations on integers at the bit level. They operate directly on the binary representation of integers, making them extremely efficient for low-level programming tasks.
Understanding bitwise operations can help optimize code performance and is useful in fields like cryptography, network programming, and embedded systems.
In this tutorial, we will cover:
- What are Bitwise Operators?
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Bitwise Left Shift (<<)
- Bitwise Right Shift (>>)
- Examples and Use Cases for Bitwise Operators
Let’s explore each bitwise operator with explanations and examples.
1. What are Bitwise Operators?
Bitwise operators work on the binary representations of integers. These operators treat numbers as a sequence of bits (0s and 1s), and each bit is operated on independently.
Here are the six bitwise operators available in Python:
- Bitwise AND (&): Sets each bit to 1 if both bits are 1.
- Bitwise OR (|): Sets each bit to 1 if one of the bits is 1.
- Bitwise XOR (^): Sets each bit to 1 if only one of the bits is 1.
- Bitwise NOT (~): Inverts all the bits.
- Left Shift (<<): Shifts bits to the left, filling the empty spots with 0.
- Right Shift (>>): Shifts bits to the right, discarding the shifted bits and filling the empty spots with the sign bit (for negative numbers) or 0 (for positive numbers).
2. Bitwise AND (&)
The bitwise AND operator (&) compares each bit of two integers. If both bits at a particular position are 1, the resulting bit is set to 1. Otherwise, it is set to 0.
Example:
a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a & b # In binary: 0001 (which is 1 in decimal) print(result) # Output: 1
- In this example, the binary representations of a and b are compared bit by bit:
- 0101 & 0011 = 0001
- The result is 1 in decimal.
3. Bitwise OR (|)
The bitwise OR operator (|) compares each bit of two integers. If either of the bits at a particular position is 1, the resulting bit is set to 1. Otherwise, it is set to 0.
Example:
a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a | b # In binary: 0111 (which is 7 in decimal) print(result) # Output: 7
- In this example, the binary representations of a and b are compared bit by bit:
- 0101 | 0011 = 0111
- The result is 7 in decimal.
4. Bitwise XOR (^)
The bitwise XOR operator (^) compares each bit of two integers. If the bits at a particular position are different, the resulting bit is set to 1. If they are the same, the resulting bit is set to 0.
Example:
a = 5 # In binary: 0101 b = 3 # In binary: 0011 result = a ^ b # In binary: 0110 (which is 6 in decimal) print(result) # Output: 6
- In this example, the binary representations of a and b are compared bit by bit:
- 0101 ^ 0011 = 0110
- The result is 6 in decimal.
5. Bitwise NOT (~)
The bitwise NOT operator (~) inverts all the bits of its operand. It flips each 0 to 1 and each 1 to 0. However, in Python, the bitwise NOT operation is implemented as -(n + 1) for a number n.
Example:
a = 5 # In binary: 0101 result = ~a # In binary: 1010 (which is -6 in decimal) print(result) # Output: -6
- In this example, the binary representation of 5 is flipped:
- ~0101 = 1010 (which is the two's complement of 5, giving -6).
6. Bitwise Left Shift (<<)
The left shift operator (<<) shifts the bits of its operand to the left by the number of positions specified. The empty positions on the right are filled with 0s, and the value is effectively multiplied by 2 for each shift.
Example:
a = 5 # In binary: 0101 result = a << 1 # In binary: 1010 (which is 10 in decimal) print(result) # Output: 10
- In this example, the bits of a are shifted one position to the left:
- 0101 << 1 = 1010
- The result is 10 in decimal.
7. Bitwise Right Shift (>>)
The right shift operator (>>) shifts the bits of its operand to the right by the number of positions specified. For positive numbers, the empty positions on the left are filled with 0s, and the value is effectively divided by 2 for each shift.
Example:
a = 5 # In binary: 0101 result = a >> 1 # In binary: 0010 (which is 2 in decimal) print(result) # Output: 2
- In this example, the bits of a are shifted one position to the right:
- 0101 >> 1 = 0010
- The result is 2 in decimal.
8. Examples and Use Cases for Bitwise Operators
Example 1: Checking if a Number is Even or Odd Using Bitwise AND
You can check if a number is even or odd by using the bitwise AND operator (&). If the last bit of a number is 1, the number is odd; otherwise, it is even.
num = 7 if num & 1: print(f"{num} is odd.") else: print(f"{num} is even.")
Output:
7 is odd.
- In this example, 7 & 1 evaluates to 1, indicating that 7 is odd.
Example 2: Swapping Two Numbers Without a Temporary Variable Using Bitwise XOR
The XOR operator can be used to swap two numbers without needing an extra temporary variable.
a = 10 b = 15 # Swap using XOR a = a ^ b b = a ^ b a = a ^ b print(f"a = {a}, b = {b}") # Output: a = 15, b = 10
Output:
a = 15, b = 10
- In this example, XOR swaps the values of a and b without using a temporary variable.
Example 3: Efficient Multiplication and Division Using Bitwise Shift Operators
You can use left shift to multiply a number by powers of 2, and right shift to divide a number by powers of 2. This is more efficient than using multiplication or division operators.
num = 4 # Multiply by 2 using left shift result_multiply = num << 1 # Equivalent to 4 * 2 = 8 # Divide by 2 using right shift result_divide = num >> 1 # Equivalent to 4 / 2 = 2 print(f"Multiply: {result_multiply}, Divide: {result_divide}")
Output:
Multiply: 8, Divide: 2
- In this example, num << 1 shifts the bits to the left to multiply by 2, and num >> 1 shifts the bits to the right to divide by 2.
Summary of Python Bitwise Operators:
Operator | Description | Example |
---|---|---|
Bitwise AND & | Sets each bit to 1 if both bits are 1. | a & b |
Bitwise OR ` | ` | Sets each bit to 1 if one of the bits is 1. |
Bitwise XOR ^ | Sets each bit to 1 if only one of |
the bits is 1. | a ^ b | | Bitwise NOT ~ | Inverts all the bits (equivalent to -(n + 1)). | ~a | | Left Shift << | Shifts bits to the left, multiplying by powers of 2. | a << 1 | | Right Shift >>| Shifts bits to the right, dividing by powers of 2. | a >> 1 |
Conclusion
Bitwise operators in Python provide a powerful tool for manipulating individual bits of integers. These operators are efficient and commonly used in low-level programming tasks such as:
- Checking if a number is even or odd
- Swapping values without a temporary variable
- Optimizing multiplication and division by powers of 2
- Performing logical operations at the bit level