英文:
What do the operators '<<' and '>>' do?
问题
我在http://tour.golang.org上跟着“GO之旅”学习。
表格15中有一些代码我无法理解。它使用以下语法定义了两个常量:
const (
Big = 1<<100
Small = Big>>99
)
对我来说,这并不清楚它的含义。我尝试修改代码并使用不同的值运行它,以记录变化,但我无法理解其中的逻辑。
然后,在表格24中再次使用了该运算符。它使用以下语法定义了一个变量:
MaxInt uint64 = 1<<64 - 1
当打印该变量时,输出为:
uint64(18446744073709551615)
其中uint64
是类型。但我无法理解18446744073709551615
是从哪里来的。
英文:
I was following 'A tour of GO` on http://tour.golang.org.
The table 15 has some code that I cannot understand. It defines two constants with the following syntax:
const (
Big = 1<<100
Small = Big>>99
)
And it's not clear at all to me what it means. I tried to modify the code and run it with different values, to record the change, but I was not able to understand what is going on there.
Then, it uses that operator again on table 24. It defines a variable with the following syntax:
MaxInt uint64 = 1<<64 - 1
And when it prints the variable, it prints:
uint64(18446744073709551615)
Where uint64
is the type. But I can't understand where 18446744073709551615
comes from.
答案1
得分: 11
它们是Go的位移运算符。
这里有一个关于它们在C中如何工作的很好的解释(它们在几种语言中的工作方式相同)。
基本上,1<<64 - 1
对应于2^64 - 1,等于18446744073709551615。
可以这样理解。在十进制中,如果你从001(即10^0)开始,然后将1向左移动,你最终得到010,即10^1。如果再次移动,你最终得到100,即10^2。因此,向左移位等于将数字乘以10,乘以移位的次数。
在二进制中,原理相同,但是是在基数为2的情况下,所以1<<64意味着将数字乘以2的64次方(即2 ^ 64)。
英文:
They are Go's bitwise shift operators.
Here's a good explanation of how they work for C (they work in the same way in several languages).
Basically 1<<64 - 1
corresponds to 2^64 -1, = 18446744073709551615.
Think of it this way. In decimal if you start from 001 (which is 10^0) and then shift the 1 to the left, you end up with 010, which is 10^1. If you shift it again you end with 100, which is 10^2. So shifting to the left is equivalent to multiplying by 10 as many times as the times you shift.
In binary it's the same thing, but in base 2, so 1<<64 means multiplying by 2 64 times (i.e. 2 ^ 64).
答案2
得分: 7
这与C语言家族中的所有语言相同:位移操作。
参见http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
这个操作通常用于将无符号整数乘以或除以2的幂次:
b := a >> 1 // 除以2
1<<100
简单地表示 2^100
(这是一个很大的数)。
1<<64-1
是 2⁶⁴-1,这是你可以在64位中表示的最大整数(顺便说一下,你不能将 1<<64
表示为一个64位整数,而表15的目的是演示在Go中你可以在数值常量中使用它)。
英文:
That's the same as in all languages of the C family : a bit shift.
See http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
This operation is commonly used to multiply or divide an unsigned integer by powers of 2 :
b := a >> 1 // divides by 2
1<<100
is simply 2^100
(that's Big).
1<<64-1
is 2⁶⁴-1, and that's the biggest integer you can represent in 64 bits (by the way you can't represent 1<<64
as a 64 bits int and the point of table 15 is to demonstrate that you can have it in numerical constants anyway in Go).
答案3
得分: 1
这是一个逻辑移位:
> 操作数中的每个位都只是向左或向右移动了给定的位数,空出的位通常用零填充
<< 左移 整数 << 无符号整数
>> 右移 整数 >> 无符号整数
英文:
It's a logical shift:
> every bit in the operand is simply moved a given number of bit
> positions, and the vacant bit-positions are filled in, usually with
> zeros
<< left shift integer << unsigned integer
>> right shift integer >> unsigned integer
答案4
得分: 1
The >> and << are logical shift operations. You can see more about those here:
http://en.wikipedia.org/wiki/Logical_shift
Also, you can check all the Go operators in their webpage
英文:
The >> and << are logical shift operations. You can see more about those here:
http://en.wikipedia.org/wiki/Logical_shift
Also, you can check all the Go operators in their webpage
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论