Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go uint, uint8 to uint64 and uintptr types

Reference for Go's unsigned integer types: sizes and ranges of uint8, uint16, uint32, uint64, uint and uintptr, wrap-around underflow and when to use them.

Unsigned integers hold only values from zero upward. Go has uint8, uint16, uint32, uint64, uint (32 or 64 bits depending on the platform) and uintptr, an integer large enough to hold a pointer's bits. byte is another name for uint8.

Unsigned arithmetic wraps around modulo 2n. Subtracting one from an unsigned zero does not give -1; it gives the largest value of the type. That is the most common source of bugs with unsigned types, and the main reason Go uses signed int for lengths and indexes.

Go
var flags uint8 = 0b1010_0001
var big uint64 = 1<<64 - 1

Try it

Go Ranges and the underflow trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Bit flags with unsigned integers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Type Size Range
uint8 (byte) 8 bits 0 to 255
uint16 16 bits 0 to 65,535
uint32 32 bits 0 to 4,294,967,295
uint64 64 bits 0 to 18,446,744,073,709,551,615
uint 32 or 64 bits same as uint32 or uint64
uintptr pointer-sized unsigned integer wide enough for a pointer's bit pattern

Zero value: 0.

Constants: math.MaxUint8, MaxUint16, MaxUint32, MaxUint64, and math.MaxUint for the platform uint. The larger ones are untyped constants too big for int, so convert them: uint64(math.MaxUint64).

Errors / edge cases:

  • Overflow and underflow wrap silently.
  • A negative constant cannot be assigned to an unsigned type (var x uint = -1 is a compile error).
  • Converting a negative signed variable to unsigned wraps: uint8(int8(-1)) is 255.
  • Division or remainder by zero panics.

Usage notes

  • Prefer int for sizes, counts and indexes. The standard library (len, copy, slicing) uses int, and a signed counter can safely go below zero.
  • Use unsigned types for bits and bytes: flags, masks, hashes, checksums, binary protocols and anything where wrap-around modulo 2n is wanted (for example, FNV hashing).
  • Subtraction is dangerous: a - b with a < b wraps. Compare first.
  • Shifts: the right operand may be any integer type; a negative signed shift count panics. See << and >>.
  • uintptr is not a pointer. The garbage collector does not follow it, so an object referenced only by a uintptr can be freed. It appears mostly with the unsafe package and syscall.
  • Bit operations are in math/bits: OnesCount, LeadingZeros, RotateLeft, Add64, Mul64.
  • Printing: %d, %x, %08b, %o. Hex of a negative number prints a minus sign, so convert to unsigned first for a two's-complement view.