按位运算符 x >> 1 且 x>> 0

发布于 2024-11-08 17:23:24 字数 500 浏览 0 评论 0原文

可能的重复:
这些运算符的作用是什么?
>>>在 JavaScript 中

有人可以解释一下按位运算符 >> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦境 2024-11-15 17:23:24
var size = (Math.random() * 100 >> 0) + 20;

<代码>>>上例中的 0 用于消除小数部分,如下所示:

  1. Math.random() 返回 0 到 0.99999999 之间的数字...
  2. 这个数字乘以 100 会得到另一个 0 到 99.999999 之间的数字。 。
  3. 该数右移 0 次 该数字被隐式转换为整数以进行移位操作;右移 0 次对结果整数的值没有任何影响。因此,您最终会得到 0 到 99 之间的整数。请注意,您可以使用 Math.floor() 函数而不是 >> 0 。
  4. 整数加上 20,结果是 20 到 119 之间的整数。
var size = (Math.random() * 100 >> 0) + 20;

>> 0 in the above example is used to eliminate the fractional portion, as follows:

  1. Math.random() returns a number between 0 and 0.99999999...
  2. This number multiplied by 100 gives you another number between 0 and 99.999999...
  3. This number is right shifted 0 times. The number is implicitly cast to an integer for the shift operation; right shifting 0 times does not have any effect on the value of the resulting integer. You thus end up with an integer between 0 and 99. Note that you could have used the Math.floor() function instead of >> 0.
  4. Add 20 to the integer, the result is an integer between 20 and 119.
凉墨 2024-11-15 17:23:24

位运算符>>表示右移。
它将二进制值向右移动(并删除最右边的位)。

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.

孤云独去闲 2024-11-15 17:23:24

运算符“>>”将变量的内容右移 1 位。正如您在示例中所示,这实际上会导致该值的整数除以 2:

   65 >> 1 = 32

假设变量始终为 32 位长。该示例接着说:

   65 decimal >> 1 = 32  or, in hex, 0x000041 >> 1 = 0x00000020

更一般地说:运算符 '>>'将其操作数(32 位整数)除以 2 的幂,其值为移位长度。因此:

  129 decimal >> 1 = 64  or  0x000081 >> 1 = 0x000040
  129 decimal >> 2 = 32  or  0x000081 >> 2 = 0x000020
  129 decimal >> 5 =  2  or  0x000081 >> 5 = 0x000002

and

  129 decimal >> 8 =  0  or: 0x000081 >> 8 = 0x000000

运算符 '<<'正如您所期望的,乘以其操作数。

我不知道 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:

   65 >> 1 = 32

Let's say that a variable is always 32 bits long. The example then says:

   65 decimal >> 1 = 32  or, in hex, 0x000041 >> 1 = 0x00000020

More generally: the operator '>>' divides its operand, as a 32-bit integer, by the power of 2 whose value is the shift length. Thus:

  129 decimal >> 1 = 64  or  0x000081 >> 1 = 0x000040
  129 decimal >> 2 = 32  or  0x000081 >> 2 = 0x000020
  129 decimal >> 5 =  2  or  0x000081 >> 5 = 0x000002

and

  129 decimal >> 8 =  0  or: 0x000081 >> 8 = 0x000000

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.

花期渐远 2024-11-15 17:23:24

按位移位运算符将输入 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

北城挽邺 2024-11-15 17:23:24

<代码>>> 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 by X 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.

淡淡的优雅 2024-11-15 17:23:24

你可以从 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.

栩栩如生 2024-11-15 17:23:24

按位运算符将表达式移动多个数字。所以在你的例子中你有
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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文