在Go语言中,>>表示右移操作符。

huangapple go评论96阅读模式
英文:

What does a >> mean in Go language?

问题

我正在寻找关于谷歌的Go语言的信息。在“Go之旅”中,他们有这段代码:

const (
    Big = 1<<100
    Small = Big>>99
)

但是<<>>代表什么意思?

您可以在http://tour.golang.org/#14上查看所有代码。

英文:

I’m looking for information on Google’s Go language. In “A Tour of Go” they have this code:

const (
    Big = 1<<100
    Small = Big>>99
)

But what do << and >> mean?

You can see all of the code at http://tour.golang.org/#14

答案1

得分: 36

它们是位移运算符x << y 表示 x 乘以 2 的 y 次方,而 x >> y 表示 x 除以 2 的 y 次方,或者等价地说,x 除以 2 的 y 次方。这些运算符通常用于操作一个值的二进制表示,就像十进制中的 10 的幂一样,乘以或除以二的幂会将数字向左或向右“移位”:

// 左移:

  13 *  2 ==    26 // 十进制
1101 * 10 == 11010 // 二进制(13 是 8 + 4 + 0 + 1)

// 右移(括号表示被丢弃的部分):

  13 /  2 ==   6[.5] // 十进制
1101 / 10 == 110[.1] // 二进制

由于你在操作整数,而右移通常会产生小数值,所以有几种处理右移结果舍入的方式。在 Go 中,右移是对无符号值进行逻辑右移,对有符号值进行算术右移。逻辑右移总是向零舍入,而算术右移总是向下舍入,即向负无穷大舍入。

英文:

They are bitwise shift operators. x &lt;&lt; y means <i>x</i>&nbsp;&times;&nbsp;2<sup><i>y</i></sup>, while x &gt;&gt; y means <i>x</i>&nbsp;&times;&nbsp;2<sup><i>&minus;y</i></sup> or, equivalently, x&nbsp;&divide;&nbsp;2<sup>y</sup>. These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by a power of two has the effect of “shifting” the digits left or right, respectively:

// Left shift:

  13 *  2 ==    26 // decimal
1101 * 10 == 11010 // binary (13 is 8 + 4 + 0 + 1)

// Right shift (brackets denote discarded portion):

  13 /  2 ==   6[.5] // decimal
1101 / 10 == 110[.1] // binary

Because you are operating on integers and a right shift typically results in fractional values, there are a couple of ways to handle how the result of a right shift is rounded. In Go, right shift is a logical shift on unsigned values and an arithmetic shift on signed values. Logical shift always rounds toward zero, while arithmetic shift always rounds down, that is, toward &minus;&infin;.

答案2

得分: 7

规范中:

算术运算符

...

&lt;&lt;   左移             整数 &lt;&lt; 无符号整数
&gt;&gt;   右移            整数 &gt;&gt; 无符号整数

稍微往下一点:

> 移位运算符将左操作数按右操作数指定的位数进行移位。如果左操作数是有符号整数,则实现算术移位;如果是无符号整数,则实现逻辑移位。移位计数没有上限。移位的行为就好像左操作数按1进行n次移位。因此,x << 1 等同于 x*2,x >> 1 等同于 x/2,但向负无穷截断。

英文:

From the specification:

Arithmetic operators

...

&lt;&lt;   left shift             integer &lt;&lt; unsigned integer
&gt;&gt;   right shift            integer &gt;&gt; unsigned integer

and a bit below:

> The shift operators shift the left operand by the shift count specified by the right operand. They implement arithmetic shifts if the left operand is a signed integer and logical shifts if it is an unsigned integer. There is no upper limit on the shift count. Shifts behave as if the left operand is shifted n times by 1 for a shift count of n. As a result, x << 1 is the same as x*2 and x >> 1 is the same as x/2 but truncated towards negative infinity.

答案3

得分: 6

这些是__位左移__和__位右移__运算符。它们与C语言及其衍生语言中的运算符相同。

x << y

表示x乘以2的y次方

x >> y

表示x除以2的y次方(小数部分被舍弃)

如果将数字视为二进制,则乘以2的幂将位向左移动(101 * 2^3变为101000),就像十进制乘以10的幂将数字向左移动一样(12340 * 10^3变为12340000)。相反,除以2的幂则将二进制表示向右移动。因此得名。顺便说一句,对于计算机来说,这是一种非常快速的操作,因此在性能关键的位操作应用程序(例如加密)中经常使用。

英文:

These are bit shift left and bit shift right operators. They are the same as in the C language and it's derivatives.

x &lt;&lt; y 

is x times 2 to the power of y

x &gt;&gt; y

is x divided by 2 to the power of y (fractional part discarded)

If you view the numbers as binary, than multiplication by a power of 2 shifts the bits to the left (101 * 2^3 becomes 101000) in the same way as in decimal multiplying by powers of 10 shift the number to the left (12340 * 10^3 becomes 12340000). The converse is true for division by powers of 2. It shifts the binary representation to the right. Hence the name. This is an extremely fast operation for a computer to perform by the way, so it is used a lot in performance critical bit twiddling applications like cryptography for example.

答案4

得分: 4

&lt;&lt;&gt;&gt;位移运算符

它们作用于一个数字的底层二进制表示,并且根据运算符右侧指定的位数将数字向左或向右移动:

1 &lt;&lt; 1 == 2
2 &lt;&lt; 1 == 4
111b &lt;&lt; 3 == 111000b
英文:

&lt;&lt; and &gt;&gt; are shift operators.

They work on the underlying binary representation of a number, and 'shift' the number left of the operator left or right by the amount of bits specified on the right of the operator:

1 &lt;&lt; 1 == 2
2 &lt;&lt; 1 == 4
111b &lt;&lt; 3 == 111000b

huangapple
  • 本文由 发表于 2012年3月21日 09:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/9797431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定