u64
stdlib/u64.hk: inherent conversion methods on u64.
impl u64
to_int
def to_int(self) -> int
Widen to the arbitrary-precision int tier. Total and exact.
7u64.to_int() => 7
_parse
def _parse(s: string) -> Option<u64>
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.
to_f64
def to_f64(self) -> f64
Widen to f64, lossy beyond 2^53 (f64 has a 53-bit mantissa), rounded to nearest.
42u64.to_f64() => 42.0f64
0u64.to_f64() => 0.0f64
bit_and
def bit_and(self, other: u64) -> u64
Per-bit AND with another u64. Total (HANKI.md §3).
12u64.bit_and(10u64) => 8u64
bit_or
def bit_or(self, other: u64) -> u64
Per-bit OR.
12u64.bit_or(10u64) => 14u64
bit_xor
def bit_xor(self, other: u64) -> u64
Per-bit XOR.
12u64.bit_xor(10u64) => 6u64
bit_not
def bit_not(self) -> u64
Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, 18446744073709551615 for an unsigned width.
0u64.bit_not() => 18446744073709551615u64
bit_shl
def bit_shl(self, n: u32) -> u64
Left shift by n bits, zero-filling the low end. Total: n is taken mod the 64-bit width, and an over-shift wraps in place of trapping.
1u64.bit_shl(3u32) => 8u64
bit_shr
def bit_shr(self, n: u32) -> u64
Right shift by n bits, logical: the high bits are zero-filled. Total: n is taken mod the 64-bit width.
12u64.bit_shr(1u32) => 6u64
checked_add
def checked_add(self, other: u64) -> Option<u64>
Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (u64 / // % also on a zero or MIN/-1 divisor) so a caller can detect it as a value. Checked addition. None where the true sum overflows u64::MAX.
100u64.checked_add(23u64) => Some(123u64)
18446744073709551615u64.checked_add(1u64) => None
checked_sub
def checked_sub(self, other: u64) -> Option<u64>
Checked subtraction. None where the difference underflows below 0.
5u64.checked_sub(3u64) => Some(2u64)
0u64.checked_sub(1u64) => None
checked_mul
def checked_mul(self, other: u64) -> Option<u64>
Checked multiplication. None where the true product overflows u64::MAX.
6u64.checked_mul(7u64) => Some(42u64)
18446744073709551615u64.checked_mul(2u64) => None
checked_neg
def checked_neg(self) -> Option<u64>
Checked negation. Unsigned: Some(0) for 0, and None otherwise.
0u64.checked_neg() => Some(0u64)
5u64.checked_neg() => None
checked_div
def checked_div(self, other: u64) -> Option<u64>
Checked truncating division. None only on a zero divisor.
10u64.checked_div(3u64) => Some(3u64)
10u64.checked_div(0u64) => None
checkedfloordiv
def checked_floor_div(self, other: u64) -> Option<u64>
Checked floor division. It equals checked_div for unsigned; None on a zero divisor.
10u64.checked_floor_div(3u64) => Some(3u64)
10u64.checked_floor_div(0u64) => None
checked_mod
def checked_mod(self, other: u64) -> Option<u64>
Checked modulo. None only on a zero divisor.
10u64.checked_mod(3u64) => Some(1u64)
10u64.checked_mod(0u64) => None
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.
255u64.to_u8() => 255u8
256u64.to_u8() => 0u8
to_u16
def to_u16(self) -> u16
Narrows to u16, truncating (modular, low 16 bits). Total; pair with try_to_u16 to detect out-of-range instead.
65535u64.to_u16() => 65535u16
65536u64.to_u16() => 0u16
to_u32
def to_u32(self) -> u32
Narrows to u32, truncating (modular, low 32 bits). Total; pair with try_to_u32 to detect out-of-range instead.
4294967295u64.to_u32() => 4294967295u32
4294967296u64.to_u32() => 0u32
trytou8
def try_to_u8(self) -> Option<u8>
Narrows to u8, returning None when the value doesn't fit.
255u64.try_to_u8() => Some(255u8)
256u64.try_to_u8() => None
trytou16
def try_to_u16(self) -> Option<u16>
Narrows to u16, returning None when the value doesn't fit.
65535u64.try_to_u16() => Some(65535u16)
65536u64.try_to_u16() => None
trytou32
def try_to_u32(self) -> Option<u32>
Narrows to u32, returning None when the value doesn't fit.
4294967295u64.try_to_u32() => Some(4294967295u32)
4294967296u64.try_to_u32() => None
to_i64
def to_i64(self) -> i64
Reinterprets the 64 bits as i64 (two's complement): same bits, total, the exact inverse of i64.to_u64. Lets a codec read a signed integer back from its wire bytes.
9u64.to_i64() => 9i64
18446744073709551615u64.to_i64() => -1i64
impl FromString<u64>
parse
def parse(s: string) -> Result<u64, ParseError>
Parses a base-10 u64. A value outside 0 ..= 18446744073709551615 is refused rather than wrapped, and a leading - is refused outright. Surrounding whitespace is trimmed.
u64.parse("42") => Ok(42u64)
u64.parse("18446744073709551615") => Ok(18446744073709551615u64)
u64.parse("18446744073709551616") => Err(ParseError(input="18446744073709551616", reason="out of range for u64 (0 ..= 18446744073709551615)"))
u64.parse("-1") => Err(ParseError(input="-1", reason="negative, and u64 is unsigned"))
u64.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))