Skip to content

int

The int module is auto-imported — no import statement needed.

FunctionSignatureDescription
to_string(Int) -> StringConvert an integer to its decimal string representation.
to_hex(Int) -> StringConvert an integer to its hexadecimal string representation (lowercase).
parse(String) -> Result[Int, String]Parse a decimal string into an integer. Returns err if the string is not a valid integer.
from_hex(String) -> Result[Int, String]Parse a hexadecimal string into an integer. Returns err if the string is not valid hex.
abs(Int) -> IntReturn the absolute value of an integer.
min(Int, Int) -> IntReturn the smaller of two integers.
max(Int, Int) -> IntReturn the larger of two integers.
band(Int, Int) -> IntBitwise AND of two integers.
bor(Int, Int) -> IntBitwise OR of two integers.
bxor(Int, Int) -> IntBitwise XOR of two integers.
bshl(Int, Int) -> IntBitwise shift left.
bshr(Int, Int) -> IntBitwise shift right (arithmetic).
bnot(Int) -> IntBitwise NOT (complement) of an integer.
wrap_add(Int, Int, Int) -> IntWrapping addition within a given bit width. Overflow wraps around.
wrap_mul(Int, Int, Int) -> IntWrapping multiplication within a given bit width. Overflow wraps around.
rotate_right(Int, Int, Int) -> IntRotate bits right within a given bit width.
rotate_left(Int, Int, Int) -> IntRotate bits left within a given bit width.
to_u32(Int) -> IntTruncate an integer to an unsigned 32-bit value (mask to 0..4294967295).
to_u8(Int) -> IntTruncate an integer to an unsigned 8-bit value (mask to 0..255).
clamp(Int, Int, Int) -> IntClamp an integer to the range [lo, hi].
to_float(Int) -> FloatConvert an integer to a floating-point number.

Convert an integer to its decimal string representation.

int.to_string(42) // => \"42\

Convert an integer to its hexadecimal string representation (lowercase).

int.to_hex(255) // => \"ff\

int.parse(s: String) -> Result[Int, String]

Section titled “int.parse(s: String) -> Result[Int, String]”

Parse a decimal string into an integer. Returns err if the string is not a valid integer.

int.parse(\"42\") // => ok(42)

int.from_hex(s: String) -> Result[Int, String]

Section titled “int.from_hex(s: String) -> Result[Int, String]”

Parse a hexadecimal string into an integer. Returns err if the string is not valid hex.

int.parse_hex(\"ff\") // => ok(255)

Return the absolute value of an integer.

int.abs(-5) // => 5

Return the smaller of two integers.

int.min(3, 7) // => 3

Return the larger of two integers.

int.max(3, 7) // => 7

Bitwise AND of two integers.

int.band(0b1100, 0b1010) // => 0b1000

Bitwise OR of two integers.

int.bor(0b1100, 0b1010) // => 0b1110

Bitwise XOR of two integers.

int.bxor(0b1100, 0b1010) // => 0b0110

Bitwise shift left.

int.bshl(1, 3) // => 8

Bitwise shift right (arithmetic).

int.bshr(8, 2) // => 2

Bitwise NOT (complement) of an integer.

int.bnot(0) // => -1

int.wrap_add(a: Int, b: Int, bits: Int) -> Int

Section titled “int.wrap_add(a: Int, b: Int, bits: Int) -> Int”

Wrapping addition within a given bit width. Overflow wraps around.

int.wrap_add(255, 1, 8) // => 0

int.wrap_mul(a: Int, b: Int, bits: Int) -> Int

Section titled “int.wrap_mul(a: Int, b: Int, bits: Int) -> Int”

Wrapping multiplication within a given bit width. Overflow wraps around.

int.wrap_mul(16, 16, 8) // => 0

int.rotate_right(a: Int, n: Int, bits: Int) -> Int

Section titled “int.rotate_right(a: Int, n: Int, bits: Int) -> Int”

Rotate bits right within a given bit width.

int.rotate_right(1, 1, 8) // => 128

int.rotate_left(a: Int, n: Int, bits: Int) -> Int

Section titled “int.rotate_left(a: Int, n: Int, bits: Int) -> Int”

Rotate bits left within a given bit width.

int.rotate_left(128, 1, 8) // => 1

Truncate an integer to an unsigned 32-bit value (mask to 0..4294967295).

int.to_u32(300) // => 300

Truncate an integer to an unsigned 8-bit value (mask to 0..255).

int.to_u8(300) // => 44

int.clamp(n: Int, lo: Int, hi: Int) -> Int

Section titled “int.clamp(n: Int, lo: Int, hi: Int) -> Int”

Clamp an integer to the range [lo, hi].

int.clamp(15, 0, 10) // => 10

Convert an integer to a floating-point number.

int.to_float(42) // => 42.0

Reinterpret an integer’s bits as an IEEE 754 float (f64).

int.bits_to_float(4607182418800017408) // => 1.0