Algorithms 101: `>>` vs `>>>`

In Python, understanding the difference between the >> operator and the non-existent >>> operator is crucial for correct bitwise operations and shifts. This section will explain how these operators work, clarify the distinction, and illustrate their use cases.

在 Python 中,理解 >> 操作符和不存在的 >>> 操作符之间的区别对于正确执行位操作和移位至关重要。本节将解释这些操作符的工作原理,澄清它们的区别,并举例说明它们的使用场景。

1. What is the >> Operator?

[English] The >> operator in Python is a bitwise right shift operator. It shifts the bits of its operand to the right by a specified number of positions. This operation effectively divides the number by 2 for each position shifted.

  • Signed Right Shift (>>): This operator shifts the bits to the right, filling the leftmost bits with the sign bit (the original leftmost bit). This means that for positive numbers, 0s are shifted in, while for negative numbers, 1s are shifted in.

Example:

# Right shift positive number
a = 0b1100  # 12 in binary
result = a >> 2
print(bin(result))  # 0b0011 -> 3 in decimal

# Right shift negative number
b = -0b1100  # -12 in binary (two's complement representation)
result = b >> 2
print(bin(result))  # -0b0011 -> -3 in decimal

What Happens: The code demonstrates the right shift operation for both positive and negative numbers.

Behind the Scenes: The right shift operator divides the number by 2 for each shift while maintaining the sign of the original number.

[Chinese] Python 中的 >> 操作符是一个位右移操作符。它将操作数的位向右移动指定的位数。每移动一位,该操作实际上将数字除以 2

  • 有符号右移 (>>): 该操作符将位向右移动,将符号位(原来的最左位)填充到左边的空位。对于正数,填充 0;对于负数,填充 1

示例:

# 右移正数
a = 0b1100  # 二进制 12
result = a >> 2
print(bin(result))  # 0b0011 -> 十进制 3

# 右移负数
b = -0b1100  # 二进制 -12 (二进制补码表示)
result = b >> 2
print(bin(result))  # -0b0011 -> 十进制 -3

What Happens: 代码展示了对正数和负数的右移操作。

Behind the Scenes: 右移操作符每次右移都会将数字除以 2,同时保持原始数字的符号。

2. What is the >>> Operator and Why Doesn’t Python Support It?

[English] The >>> operator, known as the unsigned right shift operator, exists in some other programming languages like Java and JavaScript. This operator shifts bits to the right, but instead of preserving the sign bit, it fills the leftmost bits with 0s regardless of the sign of the original number.

Python doesn’t support the >>> operator because Python integers are of arbitrary precision, meaning they can grow as large as needed without overflow, and there is no fixed bit-length. Therefore, the concept of an unsigned shift doesn’t apply in Python as it does in languages with fixed-width integers like Java.

Example in JavaScript:

// Unsigned right shift in JavaScript
let a = -12;
let result = a >>> 2;
console.log(result);  // 1073741821

What Happens: The code shifts the bits of a to the right by 2 positions, filling the leftmost bits with 0s, resulting in a large positive integer.

[Chinese] >>> 操作符,被称为无符号右移操作符,在某些其他编程语言(如 Java 和 JavaScript)中存在。这个操作符将位向右移动,但与保留符号位不同,它将左边的位无论原始数字的符号如何都填充为 0

Python 不支持 >>> 操作符,因为 Python 整数是任意精度的,这意味着它们可以在不溢出的情况下根据需要增长,并且没有固定的位长度。因此,无符号移位的概念在 Python 中不适用,因为它不像 Java 这样的语言有固定宽度的整数。

JavaScript 中的示例:

// JavaScript 中的无符号右移
let a = -12;
let result = a >>> 2;
console.log(result);  // 1073741821

What Happens: 代码将 a 的位向右移动 2 位,左边的位填充为 0,结果是一个较大的正整数。

3. When Should You Use >> and Why Doesn’t Python Need >>>?

[English] The >> operator is used in Python for performing right shifts while preserving the sign of the number, making it suitable for arithmetic operations where the sign matters.

Python doesn’t need >>> because its integers can grow indefinitely, and there is no concept of overflow or fixed bit-width that would necessitate an unsigned right shift. If you need behavior similar to >>>, you can manually achieve it by working with positive integers or by masking the result of a right shift.

Example:

# Simulating unsigned right shift in Python using masking
def unsigned_right_shift(n, shift):
    if n < 0:
        n = n + (1 << 32)  # Convert to 32-bit equivalent positive integer
    return n >> shift

a = -12
result = unsigned_right_shift(a, 2)
print(result)  # 1073741821

What Happens: The function simulates an unsigned right shift by converting the negative integer to its 32-bit positive equivalent before shifting.

Behind the Scenes: This method allows you to mimic >>> behavior in Python, though it’s usually unnecessary given Python’s handling of integers.

[Chinese] >> 操作符在 Python 中用于执行右移,同时保留数字的符号,使其适用于符号重要的算术操作。

Python 不需要 >>>,因为其整数可以无限增长,没有溢出或固定位宽的概念,从而不需要无符号右移。如果你需要类似于 >>> 的行为,你可以通过操作正整数或通过掩码右移的结果来手动实现。

示例:

# 使用掩码在 Python 中模拟无符号右移
def unsigned_right_shift(n, shift):
    if n < 0:
        n = n + (1 << 32)  # 转换为32位等效的正整数
    return n >> shift

a = -12
result = unsigned_right_shift(a, 2)
print(result)  # 1073741821

What Happens: 该函数通过在移位之前将负整数转换为其 32 位正整数等效值来模拟无符号右移。

Behind the Scenes: 这种方法允许你在 Python 中模拟 >>> 的行为,尽管鉴于 Python 对整数的处理,这通常是没有必要的。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *