在Go语言中,为什么执行”<<0″操作会得到1?

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

In Go language, why the "<<0" operation will get 1?

问题

我对go的左移位运算符感到很困惑。我运行了以下代码:

package main

func main(){
    var x = 1 << 0
    println(x)
}

然后我得到了1。但我认为结果应该是零。

英文:

I am quite confused about the go left shift operator. I run the following code:

package main

func main(){
    var x = 1 &lt;&lt; 0
    println(x)
}

And I get 1. But I think the result should be zero.

答案1

得分: 4

1 << 0 的意思是:

取 1 并不进行位移。结果应该是原始数字:1

英文:

1 << 0 means:

Take 1 and don't shift it. The result should be the original number: 1

huangapple
  • 本文由 发表于 2017年8月1日 17:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/45433505.html
匿名

发表评论

匿名网友

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

确定