按位运算符 x >> 1 且 x>> 0
有人可以解释一下按位运算符 >> 1?
示例:
65>> 1 = 32
并且当 >> 0
在这个例子中它实现了什么:
var size = (Math.random() * 100 >> 0) + 20;
Possible Duplicates:
What do these operators do?
>> in javascript
Can somebody please explain the bitwise operator >> 1
?
example:
65 >> 1 = 32
and also when >> 0
what does it achieve in this example:
var size = (Math.random() * 100 >> 0) + 20;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
<代码>>>上例中的 0 用于消除小数部分,如下所示:
Math.floor()
函数而不是>> 0 。
>> 0
in the above example is used to eliminate the fractional portion, as follows:Math.floor()
function instead of>> 0
.位运算符>>表示右移。
它将二进制值向右移动(并删除最右边的位)。
65>> 1 的二进制为:
1000001>> 1 = 100000 = 32
它有效地将数字除以 2 并舍去余数。
Bitwise operator >> means shift right.
It moves the binary value to the right (and removes the right-most bit).
65 >> 1 in binary is:
1000001 >> 1 = 100000 = 32
It effectively divides the number into 2 and drops the remainder.
运算符“>>”将变量的内容右移 1 位。正如您在示例中所示,这实际上会导致该值的整数除以 2:
假设变量始终为 32 位长。该示例接着说:
更一般地说:运算符 '>>'将其操作数(32 位整数)除以 2 的幂,其值为移位长度。因此:
and
运算符 '<<'正如您所期望的,乘以其操作数。
我不知道 Math.random( ) 是如何操作的,但我敢打赌,它的浮点返回值右移 0 会将该数字变成整数,因为只有在以下情况下左移和右移才具有算术意义:操作数是一个整数。
The operator '>>' shifts the contents of a variable right by 1 bit. This results, effectively, in integer division of that value by 2 as you show in your example:
Let's say that a variable is always 32 bits long. The example then says:
More generally: the operator '>>' divides its operand, as a 32-bit integer, by the power of 2 whose value is the shift length. Thus:
and
The operator '<<' multiplies its operand, as you'd expect.
I don't know how Math.random( ) operates, but I'm willing to bet that the shift of its floating-point returned value right by 0 turns that number into an integer, because shifting left and right has arithmetic meaning only when the operand is an integer.
按位移位运算符将输入 x 位的每一位向右移动 (>>) 或向左移动 (<<)。
65 是 1000001,因此 65>> 1 = 0100000,即 32。
编辑
以下是一些有用的链接:
http:// /en.wikipedia.org/wiki/Bitwise_operation
http://javascript.about.com /library/blbitop.htm
http://www.java2s.com /Tutorial/JavaScript/0040__Operators/ShiftLeft.htm
The bitwise shift operator shifts each bit of the input x bits to the right (>>) or to the left (<<).
65 is 1000001, thus 65 >> 1 = 0100000, which is 32.
EDIT
Here are some useful links:
http://en.wikipedia.org/wiki/Bitwise_operation
http://javascript.about.com/library/blbitop.htm
http://www.java2s.com/Tutorial/JavaScript/0040__Operators/ShiftLeft.htm
<代码>>> X 获取二进制数并将所有数字向右移动
X
位。在您的示例中,您使用 65,即二进制的 01000001。如果将其右移一位,第一个空格(左侧)将被 0 填充,最后一个数字“从末尾脱落”。给出 00100000,这是 32 的二进制表示。
>> 0
,因此将数字 0 向右移动一个空格,并且不执行任何操作。'<< X',执行相同的操作,但将数字向左移动。
这些可以与乘以 2^X(左移)或除以 2^X(右移)进行比较,但应该注意的是,二进制移位比除法运算快得多。
>> X
takes the binary number and moves all the digits right byX
places.In your example, you use 65, which is 01000001 in binary. If you shift that right by one, the first space (on the left) gets filled in with a 0, and the last digit 'falls off the end'. Giving 00100000, which is the binary representation for 32.
>> 0
, therefore shifts the number 0 spaces to the right, and does nothing.'<< X', does the same, but shifts the number to the left.
These can be compared to multiplying by 2^X (Left-shift) or divinding by 2^X (right-shift), but it should be noted that a binary shift is much faster than a division operation.
你可以从 rsplak 的帖子中理解为什么输出是 32。
>>
是右位移运算符,并将其用作>> 1
将导致每一位向右移动一位。这意味着,如果最右边的位是1,它将被排除,最左边的位将包含0。You can understand why the output is 32 from rsplak's post.
>>
is the Right Bit Shift operator and using it as>> 1
will cause every bit to be shifted one place to the right. This means, if the rightmost bit was 1, it would get expelled and the left most bit will contain 0.按位运算符将表达式移动多个数字。所以在你的例子中你有
65 是二进制 0100 0001,向右移动 1 个位置,这样就得到 0010 0000,即十进制 32。
另一个例子:
第48话3 = 6
48 十进制为 0011 0000 二进制右移 3 为 0000 0110,即十进制 6。
对于你的第二个例子,我无法帮助你 - 我无法想象为什么我会将表达式移动 0 个位置,但也许你可以找出调试它?
The bitwise operator shifts an expression by a number of digits. So in your example you have
65 which ist binary 0100 0001 shiftet 1 position to the right so you got 0010 0000 which is 32 decimal.
Another example:
48 >> 3 = 6
48 decimal is 0011 0000 binary shifted 3 to the right is 0000 0110 which is 6 decimal.
For your second example I can not help you - I can not image why I would shift an expression by 0 positions but maybe you can find out debugging it?