What does the following program print (if it runs on a 64-bit OS)?
package main
const X = '\x61' // 'a'
const Y = 0x62
const A = Y - X // 1
const B int64 = 1
var n = 32
func main() {
if A == B {
println(A << n >> n, B << n >> n)
}
}
Untyped `X` is a rune constant (in other words, its default type is `rune`, a.k.a `int32`, a 32-bit integer type).
Untyped `Y` is an int constant (in other words, its default type is `int`, a 64-bit integer type on 64-bit OSes).
At run time, the expression `A << n` overflows, so it is evaluated as 0;
on the other hand, the expression `B << n` doesn't overflow.
The Go 101 project is hosted on
Github.
Welcome to improve Go 101 articles
by submitting corrections for all kinds of mistakes,
such as typos, grammar errors, wording inaccuracies,
description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days,
please follow Go 101's official Twitter account @go100and1
or join Go 101 slack channels.
Tapir, the author of Go 101, has been on writing the Go 101 series books
and maintaining the go101.org website since 2016 July.
New contents will be continually added to the book and the website from time to time.
Tapir is also an indie game developer.
You can also support Go 101 by playing Tapir's games
(made for both Android and iPhone/iPad):
Color Infection (★★★★★), a physics based original casual puzzle game. 140+ levels.
Rectangle Pushers (★★★★★), an original casual puzzle game. Two modes, 104+ levels.