[Python] Bitwise Operators with Examples Explained

To understand bitwise operators, one must first grasp how computers represent numbers. Computers store and process information using binary numbers composed of 0s and 1s. These individual 0s and 1s are referred to as 'bits'. Bitwise operators are tools that perform operations based on these bits. Let's delve deeper into each operator.


& (AND Operator)

The AND operator yields a 1 for each position where both corresponding input bits are 1, and 0 otherwise.

Consider the numbers 13 and 10 for illustration:

  • 13 is represented in binary as 1101.
  • 10 is represented as 1010.

When we compare the bits in each position according to the AND operation:

  • Rightmost (1's place): 1 and 0 results in 0 (since both aren't 1).
  • Next (2's place): 0 and 1 results in 0 (since both aren't 1).
  • Following (4's place): 1 and 0 results in 0 (since both aren't 1).
  • Leftmost (8's place): 1 and 1 results in 1 (since both are 1).

Therefore, 1101 AND 1010 produces 1000. In decimal, this is 8.

python
a = 13
b = 10
result = a & b
print(result)  # 8

The above code displays the AND operation result of 13 and 10. The output is 8.


| (OR Operator)

The OR operator returns a 1 for each position where at least one corresponding input bit is 1.

For clarity, let's use the numbers 6 and 9:

  • 6 is represented in binary as 110.
  • 9 is represented as 1001.

When we compare the bits in each position according to the OR operation:

  • Rightmost (1's place): 0 and 1 results in 1 (since one of them is 1).
  • Next (2's place): 1 and 0 results in 1 (since one of them is 1).
  • Following (4's place): 1 and 0 results in 1 (since one of them is 1).
  • Leftmost (8's place): 0 (from 6, as it lacks a bit here) and 1 results in 1.

Consequently, 110 OR 1001 produces 1101. In decimal, this is 13.

python
a = 6
b = 9
result = a | b
print(result)  # 13

The code above portrays the OR operation result between 6 and 9. The output is 13.


^ (XOR Operator)

The XOR operator gives a 1 for each position where the corresponding input bits are different and 0 when they are the same.

Take the numbers 10 and 12 as examples:

  • 10 is represented in binary as 1010.
  • 12 is represented as 1100.

Comparing the bits in each position based on the XOR operation:

  • Rightmost (1's place): 0 and 0 results in 0 (since they are the same).
  • Next (2's place): 1 and 0 results in 1 (since they differ).
  • Following (4's place): 0 and 1 results in 1 (since they differ).
  • Leftmost (8's place): 1 and 1 results in 0 (since they are the same).

As a result, 1010 XOR 1100 yields 0110. In decimal, this translates to 6.

python
a = 10
b = 12
result = a ^ b
print(result)  # 6

The code above represents the XOR operation result between 10 and 12. The output is 6.


~ (NOT Operator)

The NOT Operator inverts all the bits in the binary representation of a given number. In essence, a 1 becomes 0 and vice versa.

Consider the number 7:

  • In binary, 7 is represented as 111.

Inverting each bit using the NOT operation:

  • Farthest right (least significant bit): 1 inverts to 0.
  • The next bit (2's place): 1 inverts to 0.
  • Farthest left (4's place): 1 inverts to 0.

Thus, the result of the NOT operation on 111 is 000. However, due to the properties of the NOT operation, positive integers become negative and vice versa, resulting in -8.

python
a = 7
result = ~a
print(result)  # -8

The above code demonstrates the result of the NOT operation on 7. The output is -8.

Note: Understanding the NOT operation requires additional knowledge of two's complement representation. Although simplified here, computers store negative numbers in two's complement format, which facilitates subtraction operations. This behavior explains the outcome of the NOT operation as illustrated above.


<< (Left Shift Operator)

The Left Shift Operator moves the bits of a number to the left by a specified amount. Bits shifted off the left end are discarded, while the right end is filled with zeros.

For instance, let's shift the number 5 to the left by 2:

  • 5 is represented in binary as 101.

Shifting 5 two bits to the left:

  • 101 becomes 10100.

Therefore, the result of shifting 101 left by 2 is 10100, which translates to 20 in decimal.

python
a = 5
result = a << 2
print(result)  # 20

The code above displays the result of shifting 5 two places to the left. The outcome is 20.


>> (Right Shift Operator)

The Right Shift Operator moves the bits of a number to the right by a designated amount. Bits shifted off the right end are discarded, while the left end inherits the original number's most significant bit (sign bit).

To illustrate, let's shift the number 18 to the right by 2:

  • 18 is denoted in binary as 10010.

Shifting 18 two bits to the right:

  • 10010 becomes 00100.

Consequently, shifting 10010 right by 2 yields 00100, which is equivalent to 4 in decimal.

python
a = 18
result = a >> 2
print(result)  # 4

The code above reveals the outcome of shifting 18 two positions to the right. The result is 4.

Note: The left and right shift operations effectively manipulate the bits of a given number. They are often employed for rapid multiplication and division operations. Such calculations are executed swiftly at the hardware level.


Utilization of Bitwise Operators

Bitwise operators find their utility beyond simple arithmetic operations and are prevalent in various domains. Their applications are especially evident in low-level programming. Below are detailed descriptions of the primary applications of bitwise operators.

1. Memory Efficiency

Bitwise operators are efficient for storing multiple flags in a single variable. As each flag can be represented by a single bit, a 32-bit integer can hold up to 32 distinct states or options. This approach is far more memory-efficient than using multiple boolean variables.

python
OPTION_A = 0b00000001  # 1
OPTION_B = 0b00000010  # 2
OPTION_C = 0b00000100  # 4

options = OPTION_A | OPTION_C  # Both OPTION_A and OPTION_C are activated

2. Rapid Computations

As bitwise operations are generally faster than arithmetic operations, they can significantly enhance computation speed in performance-critical situations. For instance, using bit shifts instead of multiplication or division can speed up calculations.

python
number = 5
double_number = number << 1  # 5 * 2 = 10
half_number = number >> 1    # 5 / 2 = 2.5

3. Data Compression and Encryption

Bitwise operators play a pivotal role in data compression and encryption.

The XOR operator returns 0 when both bits are the same and 1 otherwise. Leveraging this property, simple encryption and decryption can be achieved. For instance, XORing certain data with a key encrypts the original data. Applying XOR with the same key to the encrypted data restores the original data.

python
data = 0b10101010
key = 0b11110000

# Encryption
encrypted = data ^ key  # Result: 0b01011010

# Decryption
decrypted = encrypted ^ key  # This recovers the original data value, 0b10101010.

4. Hardware Control

Bitwise operations are of paramount importance in hardware control, particularly in embedded systems.

Setting and Clearing Bits: Specific bits in a register, often used to enable or disable certain hardware features, can be set or cleared using bitwise operations.

python
# Bit Setting (Using OR operation)
register = 0b00000000
BIT3 = 0b00001000

register |= BIT3  # Setting the 3rd bit

# Bit Clearing (Using AND and NOT operations)
register &= ~BIT3  # Clearing the 3rd bit

Bit Toggling: To reverse the state of a particular bit, the XOR operation can be employed.

python
BIT2 = 0b00000100
register ^= BIT2  # Toggling the 2nd bit

5. Masking and Filtering

Bit masks are used either to select specific bits from data or to modify certain bits.

For instance, to isolate the red, green, and blue components from a 24-bit RGB color, masks can be applied as follows:

python
color = 0x66CCFF  # An arbitrary color value

RED_MASK = 0xFF0000
GREEN_MASK = 0x00FF00
BLUE_MASK = 0x0000FF

red_component = (color & RED_MASK) >> 16
green_component = (color & GREEN_MASK) >> 8
blue_component = color & BLUE_MASK

Through such masking, individual components of an RGB color can be extracted. The efficiency and versatility of bitwise masking find broad applications in various domains such as data processing, graphics, and communication.


Bitwise operators have cemented their position as powerful tools in many programming languages, including Python. Although they might seem daunting and intricate to novices, it is hoped that the provided examples and explanations have shed light on their fundamental concepts.

These operators unveil their full potential in situations demanding optimization and efficiency. While initially, understanding and utilizing them might be challenging, with accumulated programming experience, one's grasp over the depth and breadth of bitwise operations will only strengthen. Until then, continuous learning and practice are encouraged to fortify your foundational skills.

© Copyright 2023 CLONE CODING