package main
func main() {
count := 0
for i := range [256]struct{}{} {
if n := byte(i); n == -n {
count++
}
}
println(count)
}
Choices:
Answer: 2
Run it on Go play.
Key points:
byte
(a.k.a. uint8
) non-constant non-zero value x
,
-x
overflows the range of type byte
and is wrarpped as (-x + 256) % 256
.
x
of type byte
, x == -x
happens only when x
is 0
or 128
.