Today I needed to use the maximum unsigned 64-bit integer value possible in Golang.
Here is a short program I wrote with some help from Stack Overflow to help me remember how to calculate these without any dependencies.
package main
import "fmt"
const (
minUint32 = uint32(0)
maxUint32 = ^uint32(0)
minUint64 = uint64(0)
maxUint64 = ^uint64(0)
minInt32 = int32(-maxInt32 - 1)
maxInt32 = int32(maxUint32 >> 1)
minInt64 = int64(-maxInt64 - 1)
maxInt64 = int64(maxUint64 >> 1)
)
func details[numeric int32 | int64 | uint32 | uint64](name string, num numeric) {
fmt.Printf("%9s -> dec: %21d\tbin: %65b\n", name, num, num)
}
func main() {
details("minUint32", minUint32)
details("maxUint32", maxUint32)
details("minUint64", minUint64)
details("maxUint64", maxUint64)
details("minInt32", minInt32)
details("maxInt32", maxInt32)
details("minInt64", minInt64)
details("maxInt64", maxInt64)
}
Running that shows:
minUint32 -> dec: 0 bin: 0
maxUint32 -> dec: 4294967295 bin: 11111111111111111111111111111111
minUint64 -> dec: 0 bin: 0
maxUint64 -> dec: 18446744073709551615 bin: 1111111111111111111111111111111111111111111111111111111111111111
minInt32 -> dec: -2147483648 bin: -10000000000000000000000000000000
maxInt32 -> dec: 2147483647 bin: 1111111111111111111111111111111
minInt64 -> dec: -9223372036854775808 bin: -1000000000000000000000000000000000000000000000000000000000000000
maxInt64 -> dec: 9223372036854775807 bin: 111111111111111111111111111111111111111111111111111111111111111
It turns out these are constants in the math package.