19

Maximum value of an int

 3 years ago
source link: https://yourbasic.org/golang/max-min-int-uint/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 a uint.

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

Leibniz-binary-system-1697-thumb.jpg

More code examples

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

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK