“>>>”运算符——有什么用?
">>>" operator - what is used for?
我只是在浏览 Mozilla 开发人员文档,发现我不知道其用途的符号,也无法通过互联网找到任何信息。
Array filter polyfill - line 10
var t = Object(this);
var len = t.length >>> 0;
对这个运算符的用途有什么建议吗?
这是一个 "Zero-fill right shift"(按位)运算符。
This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0
, so the result is always non-negative.
For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, 9 >>> 2
yields 2
, the same as 9 >> 2
:
However, this is not the case for negative numbers. For example, -9 >>> 2
yields 1073741821
, which is different than -9 >> 2
(which yields -3
):
@thefourtheye 关于这个运算符的用法。
TL;DR
t.length >>> 0;
实际上试图从 t.length
中获取一个有效的 32 位无符号整数。据我们所知,t.length
可以是任何类型(对象、数组、字符串等)。 >>> 0
return 如果它已经是有效的 32 位无符号数,则该值保持不变。例如,
console.log({} >>> 0);
// 0
console.log([] >>> 0);
// 0
console.log("Google" >>> 0);
// 0
通常,这种按位技巧用于避免 if
块类型检查,例如
var len = 0;
if (typeof data === 'number') {
len = data;
}
如果 len
是浮点值,我们可能仍然需要将其转换为整数。
说明
>>>
称为零填充右移运算符。除了用作按位运算符外,它还用于从对象中获取 32 位数值。 ECMA Script 5.1 Specification for >>>
表示
- Let lref be the result of evaluating ShiftExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating AdditiveExpression.
- Let rval be GetValue(rref).
- Let lnum be ToUint32(lval).
- Let rnum be ToUint32(rval).
- Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
- Return the result of performing a zero-filling right shift of lnum by shiftCount bits. Vacated bits are filled with zero. The result is an unsigned 32-bit integer.
它基本上将两个操作数都转换为 32 位无符号整数(第 5 步和第 6 步)并将左侧表达式、右侧表达式移位。
如果我们查看 definition of ToInt32
、
- Let number be the result of calling ToNumber on the input argument.
- If number is NaN, +0, −0, +∞, or −∞, return +0.
- Let posInt be sign(number) * floor(abs(number)).
- Let int32bit be posInt modulo 232; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such that the mathematical difference of posInt and k is mathematically an integer multiple of 232.
- Return int32bit.
首先将参数转换为数字(如果它不是有效数字,则 NaN
将由 ToNumber
编辑 return)。第 4 步确保您 return 是 0 到 232.
范围内的有效数字
我只是在浏览 Mozilla 开发人员文档,发现我不知道其用途的符号,也无法通过互联网找到任何信息。
Array filter polyfill - line 10
var t = Object(this);
var len = t.length >>> 0;
对这个运算符的用途有什么建议吗?
这是一个 "Zero-fill right shift"(按位)运算符。
This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes
0
, so the result is always non-negative.For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example,
9 >>> 2
yields2
, the same as9 >> 2
:However, this is not the case for negative numbers. For example,
-9 >>> 2
yields1073741821
, which is different than-9 >> 2
(which yields-3
):
@thefourtheye
TL;DR
t.length >>> 0;
实际上试图从 t.length
中获取一个有效的 32 位无符号整数。据我们所知,t.length
可以是任何类型(对象、数组、字符串等)。 >>> 0
return 如果它已经是有效的 32 位无符号数,则该值保持不变。例如,
console.log({} >>> 0);
// 0
console.log([] >>> 0);
// 0
console.log("Google" >>> 0);
// 0
通常,这种按位技巧用于避免 if
块类型检查,例如
var len = 0;
if (typeof data === 'number') {
len = data;
}
如果 len
是浮点值,我们可能仍然需要将其转换为整数。
说明
>>>
称为零填充右移运算符。除了用作按位运算符外,它还用于从对象中获取 32 位数值。 ECMA Script 5.1 Specification for >>>
表示
- Let lref be the result of evaluating ShiftExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating AdditiveExpression.
- Let rval be GetValue(rref).
- Let lnum be ToUint32(lval).
- Let rnum be ToUint32(rval).
- Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
- Return the result of performing a zero-filling right shift of lnum by shiftCount bits. Vacated bits are filled with zero. The result is an unsigned 32-bit integer.
它基本上将两个操作数都转换为 32 位无符号整数(第 5 步和第 6 步)并将左侧表达式、右侧表达式移位。
如果我们查看 definition of ToInt32
、
- Let number be the result of calling ToNumber on the input argument.
- If number is NaN, +0, −0, +∞, or −∞, return +0.
- Let posInt be sign(number) * floor(abs(number)).
- Let int32bit be posInt modulo 232; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such that the mathematical difference of posInt and k is mathematically an integer multiple of 232.
- Return int32bit.
首先将参数转换为数字(如果它不是有效数字,则 NaN
将由 ToNumber
编辑 return)。第 4 步确保您 return 是 0 到 232.