右移 (>>)

右移 (>>) 运算符返回一个数字或 BigInt,其二进制表示形式是第一个向右移动指定位数的操作数。向右移出的多余位将被丢弃,最左侧位的副本将从左侧移入。此操作也称为 "符号传播右移" 或 "算术右移",因为结果数的符号与第一个操作数的符号相同。

¥The right shift (>>) operator returns a number or BigInt whose binary representation is the first operand shifted by the specified number of bits to the right. Excess bits shifted off to the right are discarded, and copies of the leftmost bit are shifted in from the left. This operation is also called "sign-propagating right shift" or "arithmetic right shift", because the sign of the resulting number is the same as the sign of the first operand.

Try it

语法

¥Syntax

js
x >> y

描述

¥Description

>> 运算符针对两种类型的操作数进行重载:编号和 BigInt。对于数字,该运算符返回一个 32 位整数。对于 BigInts,该运算符返回一个 BigInt。它首先 将两个操作数强制转换为数值 并测试它们的类型。如果两个操作数都变成 BigInt,则执行 BigInt 右移;否则,它将两个操作数都转换为 32 位整数 并执行数字右移。如果一个操作数变为 BigInt 而另一个操作数变为数字,则抛出 TypeError

¥The >> operator is overloaded for two types of operands: number and BigInt. For numbers, the operator returns a 32-bit integer. For BigInts, the operator returns a BigInt. It first coerces both operands to numeric values and tests the types of them. It performs BigInt right shift if both operands become BigInts; otherwise, it converts both operands to 32-bit integers and performs number right shift. A TypeError is thrown if one operand becomes a BigInt but the other becomes a number.

由于新的最左位与前一个最左位具有相同的值,因此符号位(最左位)不会改变。因此命名为 "sign-propagating"。

¥Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name "sign-propagating".

该运算符对 二进制补码 中左操作数的位表示进行运算。考虑十进制(以 10 为基数)数字 9-9 的 32 位二进制表示形式:

¥The operator operates on the left operand's bit representation in two's complement. Consider the 32-bit binary representations of the decimal (base 10) numbers 9 and -9:

     9 (base 10): 00000000000000000000000000001001 (base 2)
    -9 (base 10): 11111111111111111111111111110111 (base 2)

负十进制(以 10 为基数)数 -9 的二进制表示形式是将其相反数(即二进制的 900000000000000000000000000001001)的所有位取反,并加上 1 而成。

¥The binary representation under two's complement of the negative decimal (base 10) number -9 is formed by inverting all the bits of its opposite number, which is 9 and 00000000000000000000000000001001 in binary, and adding 1.

在这两种情况下,二进制数的符号由其最左边的位给出:对于正十进制数 9,二进制表示的最左边位是 0,对于负十进制数 -9,二进制表示的最左边位是 1

¥In both cases, the sign of the binary number is given by its leftmost bit: for the positive decimal number 9, the leftmost bit of the binary representation is 0, and for the negative decimal number -9, the leftmost bit of the binary representation is 1.

给定十进制(以 10 为基数)数字 9-9 的二进制表示形式:

¥Given those binary representations of the decimal (base 10) numbers 9, and -9:

9 >> 2 产生 2:

¥9 >> 2 yields 2:

     9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)

请注意最右边的两个位 01 已被移出,最左边的位 0 的两个副本已从左侧移入。

¥Notice how two rightmost bits, 01, have been shifted off, and two copies of the leftmost bit, 0 have been shifted in from the left.

-9 >> 2 产生 -3

¥-9 >> 2 yields -3:

     -9 (base 10): 11111111111111111111111111110111 (base 2)
                   --------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)

请注意最右边的两个位 11 是如何被移出的。但就最左边的位而言:在本例中,最左边的位是 1。因此,最左边的 1 位的两个副本已从左侧移入 - 这保留了负号。

¥Notice how two rightmost bits, 11, have been shifted off. But as far as the leftmost bits: in this case, the leftmost bit is 1. So two copies of that leftmost 1 bit have been shifted in from the left — which preserves the negative sign.

二进制表示形式 11111111111111111111111111111101 等于负十进制数 -3,因为所有负整数都存储为 二进制补码,并且可以通过将正十进制数的二进制表示形式的所有位反转来计算 3,也就是 00000000000000000000000000000011,然后加一。

¥The binary representation 11111111111111111111111111111101 is equal to the negative decimal (base 10) number -3, because all negative integers are stored as two's complements, and this one can be calculated by inverting all the bits of the binary representation of the positive decimal (base 10) number 3, which is 00000000000000000000000000000011, and then adding one.

如果左操作数是超过 32 位的数字,则会丢弃最高有效位。例如,以下超过 32 位的整数将被转换为 32 位整数:

¥If the left operand is a number with more than 32 bits, it will get the most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32-bit integer:

Before: 11100110111110100000000000000110000000000001
After:              10100000000000000110000000000001

右操作数将转换为无符号 32 位整数,然后对 32 取模,因此实际移位偏移量始终是 0 到 31 之间的正整数(含 0 和 31)。例如,100 >> 32100 >> 0 相同(并生成 100),因为 32 模 32 为 0。

¥The right operand will be converted to an unsigned 32-bit integer and then taken modulo 32, so the actual shift offset will always be a positive integer between 0 and 31, inclusive. For example, 100 >> 32 is the same as 100 >> 0 (and produces 100) because 32 modulo 32 is 0.

对于 BigInts,没有截断。从概念上讲,将正 BigInt 理解为具有无限数量的前导 0 位,将负 BigInt 理解为具有无限数量的前导 1 位。

¥For BigInts, there's no truncation. Conceptually, understand positive BigInts as having an infinite number of leading 0 bits, and negative BigInts having an infinite number of leading 1 bits.

将任何数字 x 右移 0 将返回转换为 32 位整数的 x。不要使用 >> 0 将数字截断为整数;使用 Math.trunc() 代替。

¥Right shifting any number x by 0 returns x converted to a 32-bit integer. Do not use >> 0 to truncate numbers to integers; use Math.trunc() instead.

示例

¥Examples

使用右移

¥Using right shift

js
9 >> 2; // 2
-9 >> 2; // -3

9n >> 2n; // 2n

规范

Specification
ECMAScript Language Specification
# sec-signed-right-shift-operator

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看