u16
stdlib/u16.hk: inherent conversion methods on u16.
impl u16
to_int
def to_int(self) -> int
Widen to the arbitrary-precision int tier. Total and exact.
7u16.to_int() => 7
_parse
def _parse(s: string) -> Option<u16>
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.
The sign is tested first, and not folded into the range arm: an unsigned width refuses a leading - outright, and "-0" fails even though the value it denotes is in range.
bit_and
def bit_and(self, other: u16) -> u16
Per-bit AND with another u16. Total (HANKI.md §3).
12u16.bit_and(10u16) => 8u16
bit_or
def bit_or(self, other: u16) -> u16
Per-bit OR.
12u16.bit_or(10u16) => 14u16
bit_xor
def bit_xor(self, other: u16) -> u16
Per-bit XOR.
12u16.bit_xor(10u16) => 6u16
bit_not
def bit_not(self) -> u16
Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, 65535 for an unsigned width.
0u16.bit_not() => 65535u16
bit_shl
def bit_shl(self, n: u32) -> u16
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.
1u16.bit_shl(3u32) => 8u16
bit_shr
def bit_shr(self, n: u32) -> u16
Right shift by n bits, logical: the high bits are zero-filled. Total: n is taken mod the 16-bit width.
12u16.bit_shr(1u32) => 6u16
checked_add
def checked_add(self, other: u16) -> Option<u16>
Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (u16 / // % also on a zero divisor) so a caller can detect it as a value.
100u16.checked_add(23u16) => Some(123u16)
60000u16.checked_add(10000u16) => None
checked_sub
def checked_sub(self, other: u16) -> Option<u16>
Checked subtraction. None where the difference underflows below 0.
5u16.checked_sub(3u16) => Some(2u16)
0u16.checked_sub(1u16) => None
checked_mul
def checked_mul(self, other: u16) -> Option<u16>
Checked multiplication. None where the true product overflows u16.
6u16.checked_mul(7u16) => Some(42u16)
1000u16.checked_mul(100u16) => None
checked_neg
def checked_neg(self) -> Option<u16>
Checked negation. Unsigned: Some(0u16) for 0 alone, and any nonzero value negates out of range and yields None.
0u16.checked_neg() => Some(0u16)
5u16.checked_neg() => None
checked_div
def checked_div(self, other: u16) -> Option<u16>
Checked division. None only on a zero divisor.
10u16.checked_div(3u16) => Some(3u16)
10u16.checked_div(0u16) => None
checkedfloordiv
def checked_floor_div(self, other: u16) -> Option<u16>
Checked floor division. For unsigned this equals checked_div; None only on a zero divisor.
10u16.checked_floor_div(3u16) => Some(3u16)
10u16.checked_floor_div(0u16) => None
checked_mod
def checked_mod(self, other: u16) -> Option<u16>
Checked modulo. None only on a zero divisor.
10u16.checked_mod(3u16) => Some(1u16)
10u16.checked_mod(0u16) => None
to_u32
def to_u32(self) -> u32
Widens to u32. Always exact.
0u16.to_u32() => 0u32
65535u16.to_u32() => 65535u32
to_u64
def to_u64(self) -> u64
Widens to u64. Always exact.
0u16.to_u64() => 0u64
65535u16.to_u64() => 65535u64
to_u8
def to_u8(self) -> u8
Narrows to u8, truncating (modular, low 8 bits). Total; pair with try_to_u8 to detect out-of-range instead.
255u16.to_u8() => 255u8
300u16.to_u8() => 44u8
trytou8
def try_to_u8(self) -> Option<u8>
Narrows to u8, returning None when the value doesn't fit.
255u16.try_to_u8() => Some(255u8)
256u16.try_to_u8() => None
impl FromString<u16>
parse
def parse(s: string) -> Result<u16, ParseError>
Parses a base-10 u16. A value outside 0 ..= 65535 is refused rather than wrapped, and a leading - is refused outright. Surrounding whitespace is trimmed.
u16.parse("42") => Ok(42u16)
u16.parse("65535") => Ok(65535u16)
u16.parse("65536") => Err(ParseError(input="65536", reason="out of range for u16 (0 ..= 65535)"))
u16.parse("-1") => Err(ParseError(input="-1", reason="negative, and u16 is unsigned"))
u16.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))