英文:
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 << y
means <i>x</i> × 2<sup><i>y</i></sup>, while x >> y
means <i>x</i> × 2<sup><i>−y</i></sup> or, equivalently, x ÷ 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 −∞.
答案2
得分: 7
从规范中:
算术运算符
...
<< 左移 整数 << 无符号整数
>> 右移 整数 >> 无符号整数
稍微往下一点:
> 移位运算符将左操作数按右操作数指定的位数进行移位。如果左操作数是有符号整数,则实现算术移位;如果是无符号整数,则实现逻辑移位。移位计数没有上限。移位的行为就好像左操作数按1进行n次移位。因此,x << 1 等同于 x*2,x >> 1 等同于 x/2,但向负无穷截断。
英文:
From the specification:
Arithmetic operators
...
<< left shift integer << unsigned integer
>> right shift integer >> 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 << y
is x times 2 to the power of y
x >> 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
<<
和>>
是位移运算符。
它们作用于一个数字的底层二进制表示,并且根据运算符右侧指定的位数将数字向左或向右移动:
1 << 1 == 2
2 << 1 == 4
111b << 3 == 111000b
英文:
<<
and >>
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 << 1 == 2
2 << 1 == 4
111b << 3 == 111000b
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论