hanki

u32

stdlib/u32.hk: inherent conversion methods on u32.

impl u32

to_int

def to_int(self) -> int

Widen to the arbitrary-precision int tier. Total and exact.

7u32.to_int() => 7

_parse

def _parse(s: string) -> Option<u32>

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. Exact: every u32 is representable in an f64.

42u32.to_f64() => 42.0f64
0u32.to_f64()  => 0.0f64

bit_and

def bit_and(self, other: u32) -> u32

Per-bit AND with another u32. Total (HANKI.md §3).

12u32.bit_and(10u32) => 8u32

bit_or

def bit_or(self, other: u32) -> u32

Per-bit OR.

12u32.bit_or(10u32) => 14u32

bit_xor

def bit_xor(self, other: u32) -> u32

Per-bit XOR.

12u32.bit_xor(10u32) => 6u32

bit_not

def bit_not(self) -> u32

Per-bit complement (one's complement): every bit flips, and 0 becomes all-ones, 4294967295 for an unsigned width.

0u32.bit_not() => 4294967295u32

count_ones

def count_ones(self) -> u32

The number of set bits (population count).

0u32.count_ones()          => 0u32
7u32.count_ones()          => 3u32
4294967295u32.count_ones() => 32u32

bit_shl

def bit_shl(self, n: u32) -> u32

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.

1u32.bit_shl(3u32) => 8u32

bit_shr

def bit_shr(self, n: u32) -> u32

Right shift by n bits, logical: the high bits are zero-filled. Total: n is taken mod the 32-bit width.

12u32.bit_shr(1u32) => 6u32

checked_add

def checked_add(self, other: u32) -> Option<u32>

Checked arithmetic (HANKI.md §3): the bare operators wrap; these twins return None on overflow (u32 / // % also on a zero divisor) so a caller can detect it as a value.

100u32.checked_add(23u32)         => Some(123u32)
4294967295u32.checked_add(1u32)   => None

checked_sub

def checked_sub(self, other: u32) -> Option<u32>

Checked subtraction. None where the true difference underflows below 0 (u32 is unsigned).

5u32.checked_sub(3u32) => Some(2u32)
0u32.checked_sub(1u32) => None

checked_mul

def checked_mul(self, other: u32) -> Option<u32>

Checked multiplication. None where the true product overflows u32.

6u32.checked_mul(7u32)          => Some(42u32)
4294967295u32.checked_mul(2u32) => None

checked_neg

def checked_neg(self) -> Option<u32>

Checked negation. Unsigned: Some(0u32) for 0 alone, and every other value's negation underflows and yields None.

0u32.checked_neg() => Some(0u32)
5u32.checked_neg() => None

checked_div

def checked_div(self, other: u32) -> Option<u32>

Checked truncating division. None only on a zero divisor.

10u32.checked_div(3u32) => Some(3u32)
10u32.checked_div(0u32) => None

checkedfloordiv

def checked_floor_div(self, other: u32) -> Option<u32>

Checked floor division. For unsigned values it is identical to truncating division; None only on a zero divisor.

10u32.checked_floor_div(3u32) => Some(3u32)
10u32.checked_floor_div(0u32) => None

checked_mod

def checked_mod(self, other: u32) -> Option<u32>

Checked modulo. None only on a zero divisor.

10u32.checked_mod(3u32) => Some(1u32)
10u32.checked_mod(0u32) => None

to_u64

def to_u64(self) -> u64

Widens to u64. Always exact.

0u32.to_u64()          => 0u64
4294967295u32.to_u64() => 4294967295u64

to_i32

def to_i32(self) -> i32

Reinterprets the 32 bits as i32 (two's complement): same bits, total, the exact inverse of i32.to_u32. Lets a codec read a signed integer back from its wire bytes.

5u32.to_i32()          => 5i32
4294967295u32.to_i32() => -1i32

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.

255u32.to_u8() => 255u8
256u32.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.

65535u32.to_u16() => 65535u16
65536u32.to_u16() => 0u16

tocharstring

def to_char_string(self) -> string

This value read as a Unicode codepoint, as a one-character string. A value that is not a Unicode scalar (a surrogate 55296..=57343, or above 1114111) yields the empty string, since no scalar maps to it.

65u32.to_char_string() => "A"
128512u32.to_char_string().length => 4
55296u32.to_char_string() => ""

trytou8

def try_to_u8(self) -> Option<u8>

Narrows to u8, returning None when the value doesn't fit.

255u32.try_to_u8() => Some(255u8)
256u32.try_to_u8() => None

trytou16

def try_to_u16(self) -> Option<u16>

Narrows to u16, returning None when the value doesn't fit.

65535u32.try_to_u16() => Some(65535u16)
65536u32.try_to_u16() => None

impl FromString<u32>

parse

def parse(s: string) -> Result<u32, ParseError>

Parses a base-10 u32. A value outside 0 ..= 4294967295 is refused rather than wrapped, and a leading - is refused outright. Surrounding whitespace is trimmed.

u32.parse("42")   => Ok(42u32)
u32.parse("4294967295") => Ok(4294967295u32)
u32.parse("4294967296") => Err(ParseError(input="4294967296", reason="out of range for u32 (0 ..= 4294967295)"))
u32.parse("-1")   => Err(ParseError(input="-1", reason="negative, and u32 is unsigned"))
u32.parse("nope") => Err(ParseError(input="nope", reason="not a base-10 integer"))