[英]what does the symbol >> means in javascript
It's bitwise shifting. 这是按位转移的。
Let's take the number 7, which in binary is 0b00000111
让我们以数字7 0b00000111
,二进制数字为0b00000111
7 << 1
shifts it one bit to the left, giving you 0b00001110
, which is 14 7 << 1
将其向左移0b00001110
,即为0b00001110
,即14
Similarly, you can shift to the right: 7 >> 1
will cut off the last bit, giving you 0b00000011
which is 3. 同样,您可以向右移动: 7 >> 1
将截断最后一位,为0b00000011
,即3。
[Edit] [编辑]
In JavaScript, numbers are stored as floats . 在JavaScript中,数字存储为float 。 However, when shifting you need integer values, so using bit shifting on JavaScript values will convert it from float to integer. 但是,在移位时需要整数值,因此对JavaScript值使用位移位会将其从float转换为整数。
In JavaScript, shifting by 0 bits will round the number down* (integer rounding) (Better phrased: it will convert the value to integer) 在JavaScript中,将0移位将对数字进行四舍五入*(整数四舍五入) (用更好的措词:它将值转换为整数)
> a = 7.5;
7.5
> a >> 0
7
*: Unless the number is negative. *:除非数字为负。
Sidenote: since JavaScript's integers are 32-bit, avoid using bitwise shifts unless you're absolutely sure that you're not going to use large numbers. 旁注:由于JavaScript的整数是32位,因此除非绝对确定不使用大数,否则请避免使用按位移位。
[Edit 2] [编辑2]
this.length >> 0
will also make a copy of the number, instead of taking a reference to it. this.length >> 0
也将复制该数字,而不是对其进行引用。 Although I have no idea why anyone would want that. 尽管我不知道为什么有人会想要。
就像在许多其他语言中一样, >>
运算符(在<<
和>>>
)是一个按位移位 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.