Cadence Data Strucuture: Integer Deep Dive

Integers

Integers are numbers without a fractional part. They are either signed (positive, zero, or negative) or unsigned (positive or zero).

Signed integer types which check for overflow and underflow have an Int prefix and can represent values in the following ranges:

Int8: -2^7 through 2^7 - 1

Int16: -2^15 through 2^15 - 1

Int32: -2^31 through 2^31 - 1

Int64: -2^63 through 2^63 - 1

Int128: -2^127 through 2^127 - 1

Int256: -2^255 through 2^255 - 1

2. Numeric Literals

Unsigned integer types which check for overflow and underflow have a UInt prefix and can represent values in the following ranges:

UInt8: 0 through 2^8 - 1

UInt16: 0 through 2^16 - 1

UInt32: 0 through 2^32 - 1

UInt64: 0 through 2^64 - 1

UInt128: 0 through 2^128 - 1

UInt256: 0 through 2^256 - 1

Unsigned integer types which do not check for overflow and underflow, i.e. wrap around, have the Word prefix and can represent values in the following ranges:

Word8: 0 through 2^8 - 1

Word16: 0 through 2^16 - 1

Word32: 0 through 2^32 - 1

Word64: 0 through 2^64 - 1

Integer Functions

Integers have multiple built-in functions you can use.

let largeNumber = 1234567890

answer.toString() // is "1234567890"

largeNumber.toBigEndianBytes() // is [73, 150, 2, 210]

let negTwenty: Int? = Int.fromString("-20") // ok

Saturation Arithmetic

Integers and fixed-point numbers support saturation arithmetic: Arithmetic operations, such as addition or multiplications, are saturating at the numeric bounds instead of overflowing.