liveBook · Manning

Unsigned integer

As explained in Julia as a Second Language

An unsigned integer type represents nonnegative whole numbers within a fixed bit width.

An unsigned integer stores nonnegative whole numbers within a fixed bit width. Julia names these types by prefixing the width with U: UInt8, UInt16, UInt32, UInt64, and UInt128. section 2.3

Use unsigned integers with awareness of their representation and limits:

  • Check a type's limits with typemin and typemax when the available range matters. section 2.3
  • Construct a specific width explicitly, such as UInt8(13); an out-of-range conversion such as UInt8(256) raises an InexactError. section 2.3
  • Expect unsigned values to display in hexadecimal: the 8-bit value 13 appears as 0x0d, while binary, octal, hexadecimal, and decimal forms can represent the same value. section 2.3.1
  • Prefer signed integers for ordinary work; choose unsigned integers when the problem or performance requirements make their nonnegative fixed-width representation useful. section 2.6
julia> y = Int8(42)
42

julia> typeof(y)
Int8

julia> typeof(Int16(4))
Int16

julia> UInt8(256)
ERROR: InexactError: trunc(UInt8, 256)

Compare explicit integer construction with a failed conversion beyond UInt8's capacity. section 2.3

julia> Int(0b1101)
13

julia> Int(0x0d)
13

julia> UInt8(13)
0x0d

See decimal, binary, and hexadecimal spellings resolve to the same stored value. section 2.3.1

How Julia as a Second Language covers it

How deep it goes
Explains names, widths, limits, conversions, displays, and overflow context, while deferring unfamiliar number theory to an appendix.
Examples
Demonstrates UInt8 conversion, a UInt8 range error, hexadecimal display, and equivalent binary, octal, and decimal literals.
What it assumes
Assumes basic Julia number use and may require prior familiarity with signed integers, floating-point numbers, and integer overflow.
What you can do after
Evaluate/understand unsigned integer widths, bounds, conversions, and displays, then choose them deliberately rather than by default.
How it connects
Connects unsigned integers with signed integers, floating-point numbers, hexadecimal notation, binary notation, octal notation, and integer overflow.

Also explained in

Compare how 9 books explain unsigned integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.