Go Tour基础教程#13:为什么没有溢出?

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

Go Tour basics #13: how come there's no overflow?

问题

在Go Tour的基础知识,切片#13中,呈现了以下内容:

MaxInt uint64     = 1<<64 - 1

当运行示例时,它的计算结果是正确的;我只是无法理解为什么。

unit64是我所了解的Go中int的最大容器。

同时,当在表达式中使用1<<64(例如在fmt.Printf(1<<64)中)时,它会导致异常 - 也就是说,它不像其他几种编程语言中的0

然而,当在赋值运算符的右侧使用时,它的计算结果是正确的。

我在这里漏掉了什么?

英文:

In the Go Tour basics, slice #13, the following is presented:

MaxInt uint64     = 1<<64 - 1

It evaluates correctly as seen when the example gets run; I just can't seem to understand why.

unit64 is the biggest container for an int in Go as far as I understand.

At the same time 1<<64 evaluates to an exception when used in an expression (such as in fmt.Printf(1<<64)) - that is, it's not 0 as in several other programming languages.

Still, when used on the right side of the assignment operator it evaluates fine.

What am I missing here?

答案1

得分: 1

不要紧,幻灯片#15似乎解释了它:它是一个常量,因此“没有限制的大小”(至少不是64位)。我觉得这很奇怪,但作为解释来说还可以接受。

英文:

Never mind, slide #15 seems to explain it: it's a constant and as such "does not have limited size" (not to 64 bits anyway). I feel it strange but it's OK for an explanation.

huangapple
  • 本文由 发表于 2013年7月13日 00:28:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/17619614.html
匿名

发表评论

匿名网友

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

确定