Skip to content

Latest commit

 

History

History
179 lines (112 loc) · 2.99 KB

int.md

File metadata and controls

179 lines (112 loc) · 2.99 KB

int

Integer arithmetic and bitwise. auto-imported.

int.to_string(n: Int) -> String

Convert an integer to its decimal string representation.

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

int.to_hex(n: Int) -> String

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

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

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]

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

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

int.abs(n: Int) -> Int

Return the absolute value of an integer.

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

int.min(a: Int, b: Int) -> Int

Return the smaller of two integers.

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

int.max(a: Int, b: Int) -> Int

Return the larger of two integers.

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

int.band(a: Int, b: Int) -> Int

Bitwise AND of two integers.

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

int.bor(a: Int, b: Int) -> Int

Bitwise OR of two integers.

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

int.bxor(a: Int, b: Int) -> Int

Bitwise XOR of two integers.

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

int.bshl(a: Int, n: Int) -> Int

Bitwise shift left.

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

int.bshr(a: Int, n: Int) -> Int

Bitwise shift right (arithmetic).

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

int.bnot(a: Int) -> Int

Bitwise NOT (complement) of an integer.

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

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

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

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

Rotate bits left within a given bit width.

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

int.to_u32(a: Int) -> Int

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

int.to_u32(300) // => 300

int.to_u8(a: Int) -> Int

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

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

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

int.to_float(n: Int) -> Float

Convert an integer to a floating-point number.

int.to_float(42) // => 42.0

int.bits_to_float(bits: Int) -> Float

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

int.bits_to_float(4607182418800017408) // => 1.0