无符号右移 (>>>)

无符号右移 (>>>) 运算符返回一个数字,其二进制表示形式是第一个操作数向右移动指定位数。向右移出的多余位将被丢弃,并且从左侧移入零位。这个操作也称为 "零填充右移",因为符号位变成 0,所以结果数总是正数。无符号右移不接受 BigInt 值。

¥The unsigned right shift (>>>) operator returns a number 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 zero bits are shifted in from the left. This operation is also called "zero-filling right shift", because the sign bit becomes 0, so the resulting number is always positive. Unsigned right shift does not accept BigInt values.

Try it

语法

¥Syntax

js
x >>> y

描述

¥Description

与其他算术和位运算符不同,无符号右移运算符不接受 BigInt 值。这是因为它用零填充最左边的位,但从概念上讲,BigInt 具有无限数量的前导符号位,因此没有 "最左边的位" 可以用零填充。

¥Unlike other arithmetic and bitwise operators, the unsigned right shift operator does not accept BigInt values. This is because it fills the leftmost bits with zeroes, but conceptually, BigInts have an infinite number of leading sign bits, so there's no "leftmost bit" to fill with zeroes.

该运算符对 二进制补码 中左操作数的位表示进行运算。考虑十进制(以 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,零填充右移和 符号传播右移 产生相同的结果:9 >>> 2 产生 2,与 9 >> 2 相同:

¥For the positive number 9, zero-fill right shift and sign-propagating right shift yield the same result: 9 >>> 2 yields 2, the same as 9 >> 2:

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

请注意最右边的两个位 01 是如何移出的,以及两个零是如何从左侧移入的。

¥Notice how two rightmost bits, 01, have been shifted off, and two zeroes have been shifted in from the left.

但是,请注意 -9 发生的情况:-9 >> 2 (符号传播右移) 产生 -3,但 -9 >>> 2(零填充右移)产生 1073741821:

¥However, notice what happens for -9: -9 >> 2 (sign-propagating right shift) yields -3, but -9 >>> 2 (zero-fill right shift) yields 1073741821:

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

请注意最右边的两个位 11 是如何被移出的。对于 -9 >> 2 (符号传播右移),最左边 1 位的两个副本已从左侧移入,这保留了负号。另一方面,对于 -9 >>> 2(零填充右移),零已从左侧移入,因此不保留数字的负号,结果是一个(大)正数。

¥Notice how two rightmost bits, 11, have been shifted off. For -9 >> 2 (sign-propagating right shift), two copies of the leftmost 1 bit have been shifted in from the left, which preserves the negative sign. On the other hand, for -9 >>> 2 (zero-fill right shift), zeroes have instead been shifted in from the left, so the negative sign of the number is not preserved, and the result is instead a (large) positive number.

如果左操作数是超过 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.

示例

¥Examples

使用无符号右移

¥Using unsigned right shift

js
9 >>> 2; // 2
-9 >>> 2; // 1073741821

无符号右移不适用于 BigInt。

¥Unsigned right shift doesn't work with BigInts.

js
9n >>> 2n; // TypeError: BigInts have no unsigned right shift, use >> instead

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看