Maximum value of an int
yourbasic.org/golang
Go has two predeclared integer types with implementation-specific sizes:
- a
uint(unsigned integer) has either 32 or 64 bits, - an
int(signed integer) has the same size as auint.
This code computes the limit values as untyped constants.
const UintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
const (
MaxInt = 1<<(UintSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1
MinInt = -MaxInt - 1 // -1 << 31 or -1 << 63
MaxUint = 1<<UintSize - 1 // 1<<32 - 1 or 1<<64 - 1
)
The
UintSizeconstant is also available in packagemath/bits.
Further reading
More code examples
Go blueprints: code for common tasks is a collection of handy code examples.