i16
stdlib/i16.hk: inherent conversion methods on i16.
impl i16
to_int
def to_int(self) -> int
Widen to the arbitrary-precision int tier. Total and exact.
7i16.to_int() => 7
_parse
def _parse(s: string) -> Option<i16>
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.
bit_and
def bit_and(self, other: i16) -> i16
Per-bit AND with another i16. Total (HANKI.md §3).
12i16.bit_and(10i16) => 8i16
bit_or
def bit_or(self, other: i16) -> i16
Per-bit OR.
12i16.bit_or(10i16) => 14i16
bit_xor
def bit_xor(self, other: i16) -> i16
Per-bit XOR.
12i16.bit_xor(10i16) => 6i16
bit_not
def bit_not(self) -> i16
Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, -1 in two's complement.
0i16.bit_not() => -1i16
bit_shl
def bit_shl(self, n: u32) -> i16
Left shift by n bits, zero-filling the low end. Total: n is taken mod the 16-bit width, and an over-shift wraps in place of trapping.
1i16.bit_shl(3u32) => 8i16
bit_shr
def bit_shr(self, n: u32) -> i16
Right shift by n bits, arithmetic: the high bits are filled with the sign bit, which preserves the sign. Total: n mod the 16-bit width.
(-8i16).bit_shr(1u32) => -4i16
checked_add
def checked_add(self, other: i16) -> Option<i16>
Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (i16 / // % also on a zero or MIN/-1 divisor) so a caller can detect it as a value.
100i16.checked_add(23i16) => Some(123i16)
32767i16.checked_add(1i16) => None
checked_sub
def checked_sub(self, other: i16) -> Option<i16>
Checked subtraction. None where the true difference underflows i16.
100i16.checked_sub(1i16) => Some(99i16)
(-32768i16).checked_sub(1i16) => None
checked_mul
def checked_mul(self, other: i16) -> Option<i16>
Checked multiplication. None where the true product overflows i16.
6i16.checked_mul(7i16) => Some(42i16)
32767i16.checked_mul(2i16) => None
checked_neg
def checked_neg(self) -> Option<i16>
Checked negation. None only for i16::MIN, whose negation overflows.
5i16.checked_neg() => Some(-5i16)
(-32768i16).checked_neg() => None
checked_div
def checked_div(self, other: i16) -> Option<i16>
Checked truncating division. None on a zero divisor or i16::MIN / -1.
10i16.checked_div(3i16) => Some(3i16)
10i16.checked_div(0i16) => None
(-32768i16).checked_div(-1i16) => None
checkedfloordiv
def checked_floor_div(self, other: i16) -> Option<i16>
Checked floor division, rounding toward negative infinity. None on a zero divisor or i16::MIN / -1.
(-7i16).checked_floor_div(2i16) => Some(-4i16)
7i16.checked_floor_div(0i16) => None
checked_mod
def checked_mod(self, other: i16) -> Option<i16>
Checked modulo, taking the sign of the divisor. None on a zero divisor or i16::MIN / -1.
(-7i16).checked_mod(2i16) => Some(1i16)
7i16.checked_mod(0i16) => None
to_i32
def to_i32(self) -> i32
Widens to i32. Always exact.
32767i16.to_i32() => 32767i32
to_i64
def to_i64(self) -> i64
Widens to i64. Always exact.
32767i16.to_i64() => 32767i64
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.
127i16.to_i8() => 127i8
128i16.to_i8() => -128i8
trytoi8
def try_to_i8(self) -> Option<i8>
Narrows to i8, returning None when the value doesn't fit.
127i16.try_to_i8() => Some(127i8)
128i16.try_to_i8() => None
abs
def abs(self) -> i16
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.
(-5i16).abs() => 5i16
5i16.abs() => 5i16
impl FromString<i16>
parse
def parse(s: string) -> Result<i16, ParseError>
Parses a base-10 i16. A value outside -32768 ..= 32767 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.
i16.parse("42") => Ok(42i16)
i16.parse("-32768") => Ok(-32768i16)
i16.parse("32768") => Err(ParseError(input="32768", reason="out of range for i16 (-32768 ..= 32767)"))
i16.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))