英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论