Maximum value of an int

yourbasic.org/golang

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

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 UintSize constant is also available in package math/bits.

Further reading

Bitwise operators cheat sheet

More code examples

Go blueprints: code for com­mon tasks is a collection of handy code examples.

Share this page: