Bitwise Operators

basic
Published

November 13, 2024

Bitwise operators are fundamental tools in programming that manipulate individual bits within an integer. While often overlooked, they offer powerful capabilities for efficient data manipulation, especially in areas like low-level programming, cryptography, and data compression. This guide looks into the six core bitwise operators in Python, providing clear explanations and practical code examples to your understanding.

Understanding Bits and Bytes

Before we discuss the operators, let’s quickly revisit the basics. A bit is the smallest unit of data, representing either 0 or 1. Eight bits make up a byte. Bitwise operators work directly on the binary representation of numbers.

The Six Bitwise Operators

Python supports six primary bitwise operators:

  • & (AND): Performs a logical AND operation on each pair of corresponding bits. A bit in the result is 1 only if both corresponding bits in the operands are 1.
a = 10  # Binary: 1010
b = 4   # Binary: 0100
result = a & b  # Binary: 0000 (Decimal: 0)
print(f"{a} & {b} = {result}")
  • | (OR): Performs a logical OR operation. A bit in the result is 1 if at least one of the corresponding bits in the operands is 1.
a = 10  # Binary: 1010
b = 4   # Binary: 0100
result = a | b  # Binary: 1110 (Decimal: 14)
print(f"{a} | {b} = {result}")
  • ^ (XOR): Performs a logical XOR (exclusive OR) operation. A bit in the result is 1 if exactly one of the corresponding bits in the operands is 1.
a = 10  # Binary: 1010
b = 4   # Binary: 0100
result = a ^ b  # Binary: 1110 (Decimal: 14)
print(f"{a} ^ {b} = {result}")
  • ~ (NOT): Performs a bitwise NOT operation, inverting each bit (0 becomes 1, and 1 becomes 0). Note that this operation is typically performed on a two’s complement representation, leading to a slightly counterintuitive result.
a = 10  # Binary: 1010
result = ~a  # Binary: -11 (Decimal: -11)  Two's complement representation
print(f"~{a} = {result}")
  • << (Left Shift): Shifts the bits of the left operand to the left by the number of positions specified by the right operand. New bits on the right are filled with 0s.
a = 10  # Binary: 1010
result = a << 2  # Binary: 101000 (Decimal: 40)
print(f"{a} << 2 = {result}")
  • >> (Right Shift): Shifts the bits of the left operand to the right by the number of positions specified by the right operand. The bits shifted off the right are discarded. For positive numbers, new bits on the left are filled with 0s; for negative numbers, the behavior depends on the system (often filled with 1s).
a = 10  # Binary: 1010
result = a >> 1  # Binary: 101 (Decimal: 5)
print(f"{a} >> 1 = {result}")

Practical Applications

Bitwise operators are not just for theoretical exercises. They find practical uses in:

  • Setting, clearing, or toggling individual bits: Useful in working with flags or status registers.
  • Efficient arithmetic operations: Certain operations (like multiplication or division by powers of 2) can be implemented more efficiently using bit shifts.
  • Cryptography: Bitwise operations are components in various cryptographic algorithms.
  • Data compression: Bit manipulation can reduce storage space by representing data more compactly.

This exploration of Python’s bitwise operators provides a strong foundation. Experimentation and application in various programming contexts will solidify your understanding and unveil their true power.