Go语言中的<<和>>运算符

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

Go << and >> operators

问题

请问有人可以向我解释一下在Go语言中&lt;&lt;&gt;&gt;的用法吗?我猜它和其他一些语言类似。

英文:

Could someone please explain to me the usage of &lt;&lt; and &gt;&gt; in Go? I guess it is similar to some other languages.

答案1

得分: 355

超级(可能过于简化的)定义就是<<用于“乘以2”,而>>用于“除以2” - 后面的数字表示乘除的次数。

所以n << x表示“n乘以2,x次”。而y >> z表示“y除以2,z次”。

例如,1 << 5表示“1乘以2,5次”或32。而32 >> 5表示“32除以2,5次”或1。

英文:

The super (possibly over) simplified definition is just that &lt;&lt; is used for "times 2" and &gt;&gt; is for "divided by 2" - and the number after it is how many times.

So n &lt;&lt; x is "n times 2, x times". And y &gt;&gt; z is "y divided by 2, z times".

For example, 1 &lt;&lt; 5 is "1 times 2, 5 times" or 32. And 32 &gt;&gt; 5 is "32 divided by 2, 5 times" or 1.

答案2

得分: 136

从http://golang.org/doc/go_spec.html的规范来看,至少对于整数来说,这是一个二进制移位。例如,二进制0b00001000 >> 1将变为0b00000100,而0b00001000 << 1将变为0b00010000。


Go显然不接受二进制整数的0b表示法。我只是用它来举例。在十进制中,8 >> 1等于4,8 << 1等于16。向左移动一位相当于乘以2,向右移动一位相当于除以2,舍弃任何余数。

英文:

From the spec at http://golang.org/doc/go_spec.html, it seems that at least with integers, it's a binary shift. for example, binary 0b00001000 >> 1 would be 0b00000100, and 0b00001000 << 1 would be 0b00010000.


Go apparently doesn't accept the 0b notation for binary integers. I was just using it for the example. In decimal, 8 >> 1 is 4, and 8 << 1 is 16. Shifting left by one is the same as multiplication by 2, and shifting right by one is the same as dividing by two, discarding any remainder.

答案3

得分: 35

<< 和 >> 运算符是Go算术运算符

<<   左移             整数 << 无符号整数
>>   右移            整数 >> 无符号整数

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

英文:

The << and >> operators are Go Arithmetic Operators.

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

> 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. The
> shift count must be 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.

答案4

得分: 21

以下是翻译好的内容:

它们基本上是算术运算符,在其他语言中也是一样的,这里是一个基本的PHP、C、Go示例。

GO

package main

import (
	"fmt"
)

func main() {
	var t, i uint
	t, i = 1, 1

	for i = 1; i < 10; i++ {
		fmt.Printf("%d << %d = %d \n", t, i, t<<i)
	}

	fmt.Println()

	t = 512
	for i = 1; i < 10; i++ {
		fmt.Printf("%d >> %d = %d \n", t, i, t>>i)
	}

}

GO示例

C

#include <stdio.h>
int main()
{

	int t = 1;
	int i = 1;
	
	for(i = 1; i < 10; i++) {
		printf("%d << %d = %d \n", t, i, t << i);
	}
	
        printf("\n");
	
	t = 512;
	
	for(i = 1; i < 10; i++) {
		printf("%d >> %d = %d \n", t, i, t >> i);
	}    

  return 0;
}

C示例

PHP

$t = $i = 1;

for($i = 1; $i < 10; $i++) {
	printf("%d << %d = %d \n", $t, $i, $t << $i);
}

print PHP_EOL;

$t = 512;

for($i = 1; $i < 10; $i++) {
	printf("%d >> %d = %d \n", $t, $i, $t >> $i);
}

PHP示例

它们都会输出

1 << 1 = 2 
1 << 2 = 4 
1 << 3 = 8 
1 << 4 = 16 
1 << 5 = 32 
1 << 6 = 64 
1 << 7 = 128 
1 << 8 = 256 
1 << 9 = 512 

512 >> 1 = 256 
512 >> 2 = 128 
512 >> 3 = 64 
512 >> 4 = 32 
512 >> 5 = 16 
512 >> 6 = 8 
512 >> 7 = 4 
512 >> 8 = 2 
512 >> 9 = 1 
英文:

They are basically Arithmetic operators and its the same in other languages here is a basic PHP , C , Go Example

GO

package main

import (
	&quot;fmt&quot;
)

func main() {
	var t , i uint
	t , i = 1 , 1

	for i = 1 ; i &lt; 10 ; i++ {
		fmt.Printf(&quot;%d &lt;&lt; %d = %d \n&quot;, t , i , t&lt;&lt;i)
	}


	fmt.Println()

	t = 512
	for i = 1 ; i &lt; 10 ; i++ {
		fmt.Printf(&quot;%d &gt;&gt; %d = %d \n&quot;, t , i , t&gt;&gt;i)
	}

}

GO Demo

C

#include &lt;stdio.h&gt;
int main()
{

	int t = 1 ;
	int i = 1 ;
	
	for(i = 1; i &lt; 10; i++) {
		printf(&quot;%d &lt;&lt; %d = %d \n&quot;, t, i, t &lt;&lt; i);
	}
	
        printf(&quot;\n&quot;);
	
	t = 512;
	
	for(i = 1; i &lt; 10; i++) {
		printf(&quot;%d &gt;&gt; %d = %d \n&quot;, t, i, t &gt;&gt; i);
	}    

  return 0;
}

C Demo

PHP

$t = $i = 1;

for($i = 1; $i &lt; 10; $i++) {
	printf(&quot;%d &lt;&lt; %d = %d \n&quot;, $t, $i, $t &lt;&lt; $i);
}

print PHP_EOL;

$t = 512;

for($i = 1; $i &lt; 10; $i++) {
	printf(&quot;%d &gt;&gt; %d = %d \n&quot;, $t, $i, $t &gt;&gt; $i);
}

PHP Demo

They would all output

1 &lt;&lt; 1 = 2 
1 &lt;&lt; 2 = 4 
1 &lt;&lt; 3 = 8 
1 &lt;&lt; 4 = 16 
1 &lt;&lt; 5 = 32 
1 &lt;&lt; 6 = 64 
1 &lt;&lt; 7 = 128 
1 &lt;&lt; 8 = 256 
1 &lt;&lt; 9 = 512 

512 &gt;&gt; 1 = 256 
512 &gt;&gt; 2 = 128 
512 &gt;&gt; 3 = 64 
512 &gt;&gt; 4 = 32 
512 &gt;&gt; 5 = 16 
512 &gt;&gt; 6 = 8 
512 &gt;&gt; 7 = 4 
512 &gt;&gt; 8 = 2 
512 &gt;&gt; 9 = 1 

答案5

得分: 13

n << x = n * 2^x &nbsp;&nbsp;例子: 3 << 5 = 3 * 2^5 = 96

y >> z = y / 2^z &nbsp;&nbsp;例子: 512 >> 4 = 512 / 2^4 = 32

英文:

n << x = n * 2^x &nbsp;&nbsp;Example: 3 << 5 = 3 * 2^5 = 96

y >> z = y / 2^z &nbsp;&nbsp;Example: 512 >> 4 = 512 / 2^4 = 32

答案6

得分: 8

&lt;&lt; 是左移操作符。当左操作数是有符号整数时,&gt;&gt; 是带符号右移操作符,当左操作数是无符号整数时,&gt;&gt; 是无符号右移操作符。

为了更好地理解 &gt;&gt;,可以考虑以下示例:

var u uint32 = 0x80000000;
var i int32 = -2;

u &gt;&gt; 1;  // 结果为 0x40000000,类似于 Java 中的 &gt;&gt;&gt;
i &gt;&gt; 1;  // 结果为 -1,类似于 Java 中的 &gt;&gt;

因此,当应用于无符号整数时,左侧的位被填充为零,而当应用于有符号整数时,左侧的位被填充为最左边的位(根据二进制补码表示法,当有符号整数为负数时,最左边的位为1)。

英文:

&lt;&lt; is left shift. &gt;&gt; is sign-extending right shift when the left operand is a signed integer, and is zero-extending right shift when the left operand is an unsigned integer.

To better understand &gt;&gt; think of

var u uint32 = 0x80000000;
var i int32 = -2;

u &gt;&gt; 1;  // Is 0x40000000 similar to &gt;&gt;&gt; in Java
i &gt;&gt; 1;  // Is -1 similar to &gt;&gt; in Java

So when applied to an unsigned integer, the bits at the left are filled with zero, whereas when applied to a signed integer, the bits at the left are filled with the leftmost bit (which is 1 when the signed integer is negative as per 2's complement).

答案7

得分: 8

Go的<<和>>与其他语言中的移位(即:除法或乘法的2的幂)类似,但由于Go是比C/C++更安全的语言,所以当移位计数是一个数字时,它会做一些额外的工作。

x86 CPU中的移位指令只考虑移位计数的5位(64位x86 CPU上的6位)。在C/C++等语言中,移位运算符会转换为单个CPU指令。

以下是Go代码的示例:

x := 10
y := uint(1025)  // 一个很大的移位计数
println(x >> y)
println(x << y)

输出结果为:

0
0

而C/C++程序将输出:

5
20
英文:

Go's << and >> are similar to shifts (that is: division or multiplication by a power of 2) in other languages, but because Go is a safer language than C/C++ it does some extra work when the shift count is a number.

Shift instructions in x86 CPUs consider only 5 bits (6 bits on 64-bit x86 CPUs) of the shift count. In languages like C/C++, the shift operator translates into a single CPU instruction.

The following Go code

x := 10
y := uint(1025)  // A big shift count
println(x &gt;&gt; y)
println(x &lt;&lt; y)

prints

0
0

while a C/C++ program would print

5
20

答案8

得分: 7

十进制数学中,当我们乘以或除以10时,会影响数字末尾的零。

二进制中,2具有相同的效果。因此,我们在末尾添加一个零,或者删除最后一位数字。

英文:

In decimal math, when we multiply or divide by 10, we effect the zeros on the end of the number.

In binary, 2 has the same effect. So we are adding a zero to the end, or removing the last digit

答案9

得分: 1

<< 是位左移运算符,它将相应整数的位向左移动...移位后,最右边的位为'0'。

例如:

在gcc中,我们有4字节整数,即32位。

例如,3的二进制表示为

00000000 00000000 00000000 00000011

3<<1 将得到

00000000 00000000 00000000 00000110,即6。

通常情况下,1<<x 将给出2^x。

在gcc中,

1<<20 将给出2^20,即1048576。

但在tcc中,它将给出0作为结果,因为tcc中的整数是2字节。

简单来说,在golang中,我们可以这样理解:

n << x 是 "n 乘以 2 的 x 次方"。而 y >> z 是 "y 除以 2 的 z 次方"。

n << x = n * 2^x 例如: 3<< 5 = 3 * 2^5 = 96

y >> z = y / 2^z 例如: 512 >> 4 = 512 / 2^4 = 32

英文:

<< is the bitwise left shift operator ,which shifts the bits of corresponding integer to the left….the rightmost bit being ‘0’ after the shift .

For example:

In gcc we have 4 bytes integer which means 32 bits .

like binary representation of 3 is

00000000 00000000 00000000 00000011

3<<1 would give

00000000 00000000 00000000 00000110 which is 6.

In general 1<<x would give you 2^x

In gcc

1<<20 would give 2^20 that is 1048576

but in tcc it would give you 0 as result because integer is of 2 bytes in tcc.

in simple terms we can take it like this in golang
So

n << x is "n times 2, x times". And y >> z is "y divided by 2, z times".

n << x = n * 2^x Example: 3<< 5 = 3 * 2^5 = 96

y >> z = y / 2^z Example: 512 >> 4 = 512 / 2^4 = 32

答案10

得分: 0

这些是右位运算和左位运算符。

英文:

These are Right bitwise and left bitwise operators

huangapple
  • 本文由 发表于 2011年4月27日 15:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/5801008.html
匿名

发表评论

匿名网友

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

确定