Go: What's the maximum value of an int?

Go has two predeclared integer types with implementation-specific sizes:

  • a uint has either 32 or 64 bits;
  • an int has the same size as a uint.

This code computes the limit values as untyped constants:

const BitsPerWord = 32 << (^uint(0) >> 63) // either 32 or 64

const (
	MaxInt  = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1
	MinInt  = -MaxInt - 1            // either -1 << 31 or -1 << 63
	MaxUint = 1<<BitsPerWord - 1     // either 1<<32 - 1 or 1<<64 - 1
)

Comments

Be the first to comment!