i32
stdlib/i32.hk: inherent methods and assoc fns on i32.
impl i32
to_int
def to_int(self) -> int
Widen to the arbitrary-precision int tier. Total and exact.
7i32.to_int() => 7
(-3i32).to_int() => -3
bit_and
def bit_and(self, other: i32) -> i32
Per-bit AND with another i32. Total (HANKI.md §3).
12i32.bit_and(10i32) => 8i32
bit_or
def bit_or(self, other: i32) -> i32
Per-bit OR.
12i32.bit_or(10i32) => 14i32
bit_xor
def bit_xor(self, other: i32) -> i32
Per-bit XOR.
12i32.bit_xor(10i32) => 6i32
bit_not
def bit_not(self) -> i32
Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, -1 in two's complement.
0i32.bit_not() => -1i32
bit_shl
def bit_shl(self, n: u32) -> i32
Left shift by n bits, zero-filling the low end. Total: n is taken mod the 32-bit width, and an over-shift wraps in place of trapping.
1i32.bit_shl(3u32) => 8i32
bit_shr
def bit_shr(self, n: u32) -> i32
Right shift by n bits, arithmetic: the high bits are filled with the sign bit, which preserves the sign. Total: n mod the 32-bit width.
(-8i32).bit_shr(1u32) => -4i32
checked_add
def checked_add(self, other: i32) -> Option<i32>
Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (i32 / // % also on a zero or MIN/-1 divisor) so a caller can detect it as a value.
100i32.checked_add(23i32) => Some(123i32)
2147483647i32.checked_add(1i32) => None
checked_sub
def checked_sub(self, other: i32) -> Option<i32>
Checked subtraction. None where the true difference underflows i32.
100i32.checked_sub(1i32) => Some(99i32)
(-2147483648i32).checked_sub(1i32) => None
checked_mul
def checked_mul(self, other: i32) -> Option<i32>
Checked multiplication. None where the true product overflows i32.
6i32.checked_mul(7i32) => Some(42i32)
2147483647i32.checked_mul(2i32) => None
checked_neg
def checked_neg(self) -> Option<i32>
Checked negation. None only for i32::MIN, whose negation overflows.
5i32.checked_neg() => Some(-5i32)
(-2147483648i32).checked_neg() => None
checked_div
def checked_div(self, other: i32) -> Option<i32>
Checked truncating division. None on a zero divisor or i32::MIN / -1.
10i32.checked_div(3i32) => Some(3i32)
10i32.checked_div(0i32) => None
(-2147483648i32).checked_div(-1i32) => None
checkedfloordiv
def checked_floor_div(self, other: i32) -> Option<i32>
Checked floor division, rounding toward negative infinity. None on a zero divisor or i32::MIN / -1.
(-7i32).checked_floor_div(2i32) => Some(-4i32)
7i32.checked_floor_div(0i32) => None
checked_mod
def checked_mod(self, other: i32) -> Option<i32>
Checked modulo, taking the sign of the divisor. None on a zero divisor or i32::MIN / -1.
(-7i32).checked_mod(2i32) => Some(1i32)
7i32.checked_mod(0i32) => None
to_f64
def to_f64(self) -> f64
Converts to f64. Lossless for every i32 (the f64 mantissa covers the full 32-bit range).
42i32.to_f64() => 42.0f64
(-42i32).to_f64() => -42.0f64
_parse
def _parse(s: string) -> Option<i32>
The runtime seam under [FromString]: the raw yes-or-no parse, with no room to say why. parse below is the surface, and turns the None into a reason.
_reason
def _reason(s: string) -> string
A fixed-width parse fails two ways and the reason has to say which. int is the arbitrary-precision tier, and it accepts the texts that denote a whole number, which is the discriminator.
to_i64
def to_i64(self) -> i64
Widens to i64. Always exact.
2147483647i32.to_i64() => 2147483647i64
to_u32
def to_u32(self) -> u32
Reinterprets the 32-bit two's-complement pattern as u32: same bits, total, the exact inverse of u32.to_i32. The seam a binary codec uses to reach a signed integer's wire bytes as an unsigned value.
5i32.to_u32() => 5u32
(-1i32).to_u32() => 4294967295u32
hash_u64
def hash_u64(self) -> u64
A 64-bit hash of the bit pattern, total, negatives included. Backs Hash<i32>; equal values hash equal.
5i32.hash_u64() == 5i32.hash_u64() => true
to_i8
def to_i8(self) -> i8
Narrows to i8, truncating (modular, low 8 bits). Total; pair with try_to_i8 to detect out-of-range instead.
127i32.to_i8() => 127i8
200i32.to_i8() => -56i8
to_i16
def to_i16(self) -> i16
Narrows to i16, truncating (modular, low 16 bits). Total; pair with try_to_i16 to detect out-of-range instead.
32767i32.to_i16() => 32767i16
32768i32.to_i16() => -32768i16
trytoi8
def try_to_i8(self) -> Option<i8>
Narrows to i8, returning None when the value doesn't fit.
127i32.try_to_i8() => Some(127i8)
128i32.try_to_i8() => None
trytoi16
def try_to_i16(self) -> Option<i16>
Narrows to i16, returning None when the value doesn't fit.
32767i32.try_to_i16() => Some(32767i16)
32768i32.try_to_i16() => None
abs
def abs(self) -> i32
The absolute value. Follows the fixed-width wrap semantic (HANKI.md §3, bare ops wrap): the most negative value has no positive counterpart at this width, and its abs therefore wraps to itself.
(-5i32).abs() => 5i32
5i32.abs() => 5i32
impl FromString<i32>
parse
def parse(s: string) -> Result<i32, ParseError>
Parses a base-10 i32. A value outside -2147483648 ..= 2147483647 is refused rather than wrapped: choosing a width is choosing a range, and a parse that wrapped would hand back a number the text never said. Surrounding whitespace is trimmed.
i32.parse("42") => Ok(42i32)
i32.parse("-2147483648") => Ok(-2147483648i32)
i32.parse("2147483648") => Err(ParseError(input="2147483648", reason="out of range for i32 (-2147483648 ..= 2147483647)"))
i32.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))