silk.num.u32.
Thirty-two-bit unsigned integers for fixed-width counts, masks, and binary fields.
When to use#
Use u32 when an unsigned 32-bit representation is part of the contract, including masks and
encoded fields. Use usize for collection positions and allocation sizes tied to the target.
Details#
Each arithmetic operation has four forms with different overflow rules:
The plain form, such as
add, traps on overflow. Use it when overflow is a program error.The
checkedform, such ascheckedAdd, returnsNoneon overflow. Use it when input data controls the operands.The
wrappingform, such aswrappingAdd, computes the result modulo 2^32.The
saturatingform, such assaturatingAdd, returnsMINorMAXon overflow.
Division and remainder by zero trap. checkedDivide and checkedRemainder return None.
The shift count must be in the range 0 to BITS - 1. Otherwise, the program traps. A rotate
reduces its count modulo BITS, so each count is valid. A right shift inserts zero bits.
A to conversion, such as toU8, traps if the value is out of the range of the destination
type. A checkedTo conversion returns None instead. Conversion to f32 or f64 rounds to
the nearest value if the integer has more significant bits than the float.
A wrappingTo conversion, such as wrappingToI16, never traps. It keeps the low bits of the
two's complement form of the value, like a cast in C or Rust, so it truncates, sign-extends, or
reinterprets bits.
countOnes, leadingZeros, and trailingZeros count the bits of the value. For
zero, both zero counts return BITS.
parse reads decimal text. A sign is not valid. To write a value as text, use
silk.text.format.Format.display or Format.displayWith with a Writer. Neither needs an
Allocator.
Examples#
Extract one byte from a fixed-width field#
import silk.num.u32
pub fn main() -> i32 {
let shifted = u32.shiftRight(0x00002A00, 8)
let byte = u32.bitAnd(shifted, 0xFF)
let result = u32.toI32(byte)
if result != 42 {
return 1
}
return 0
}Parse decimal text and reject invalid text#
import silk.text.format { ParseError }
import silk.core.result { Result }
import silk.num.u32
pub fn main() -> i32 {
let parsed = u32.parse("42") |> Result<u32, ParseError>.unwrapOr(0)
let invalid = u32.parse("4x2") |> Result<u32, ParseError>.unwrapOr(0)
if parsed != 42 || invalid != 0 {
return 1
}
return 0
}Import as u32 with import silk.num.u32.
Public declarations: 70.
MAX#
pub const MAX: u32The largest u32 value.
MIN#
pub const MIN: u32The smallest u32 value.
BITS#
pub const BITS: u32The fixed width of u32, in bits.
toU8#
pub fn toU8(value: u32) -> u8Converts value to u8. Traps if value is outside the u8 range. Use
this function when an out-of-range value is a program error.
checkedToU8#
pub fn checkedToU8(value: u32) -> Option<u8>Converts value to u8, or returns None if value is outside the u8
range. Use this function when an out-of-range value is input data.
toU16#
pub fn toU16(value: u32) -> u16Converts value to u16. Traps if value is outside the u16 range. Use
this function when an out-of-range value is a program error.
checkedToU16#
pub fn checkedToU16(value: u32) -> Option<u16>Converts value to u16, or returns None if value is outside the u16
range. Use this function when an out-of-range value is input data.
toU32#
pub fn toU32(value: u32) -> u32Returns value unchanged as u32. Use this function when generic conversion code
can select u32 as both source and destination.
checkedToU32#
pub fn checkedToU32(value: u32) -> Option<u32>Returns Some with value unchanged as u32. Use this function when generic
checked-conversion code can select the same source and destination type.
toU64#
pub fn toU64(value: u32) -> u64Converts value exactly to u64. Every u32 value is representable.
checkedToU64#
pub fn checkedToU64(value: u32) -> Option<u64>Converts value exactly to u64 and returns Some. Every u32 value is
representable.
toUsize#
pub fn toUsize(value: u32) -> usizeConverts value exactly to usize. Every u32 value is representable.
checkedToUsize#
pub fn checkedToUsize(value: u32) -> Option<usize>Converts value exactly to usize and returns Some. Every u32 value is
representable.
toI8#
pub fn toI8(value: u32) -> i8Converts value to i8. Traps if value is outside the i8 range. Use
this function when an out-of-range value is a program error.
checkedToI8#
pub fn checkedToI8(value: u32) -> Option<i8>Converts value to i8, or returns None if value is outside the i8
range. Use this function when an out-of-range value is input data.
toI16#
pub fn toI16(value: u32) -> i16Converts value to i16. Traps if value is outside the i16 range. Use
this function when an out-of-range value is a program error.
checkedToI16#
pub fn checkedToI16(value: u32) -> Option<i16>Converts value to i16, or returns None if value is outside the i16
range. Use this function when an out-of-range value is input data.
toI32#
pub fn toI32(value: u32) -> i32Converts value to i32. Traps if value is outside the i32 range. Use
this function when an out-of-range value is a program error.
checkedToI32#
pub fn checkedToI32(value: u32) -> Option<i32>Converts value to i32, or returns None if value is outside the i32
range. Use this function when an out-of-range value is input data.
toI64#
pub fn toI64(value: u32) -> i64Converts value exactly to i64. Every u32 value is representable.
checkedToI64#
pub fn checkedToI64(value: u32) -> Option<i64>Converts value exactly to i64 and returns Some. Every u32 value is
representable.
toIsize#
pub fn toIsize(value: u32) -> isizeConverts value to isize. Traps if value is outside the isize range. Use
this function when an out-of-range value is a program error.
checkedToIsize#
pub fn checkedToIsize(value: u32) -> Option<isize>Converts value to isize, or returns None if value is outside the isize
range. Use this function when an out-of-range value is input data.
toF32#
pub fn toF32(value: u32) -> f32Converts value to the nearest f32 value, with ties to even. Only magnitudes below 2^24 are
exactly representable, so a larger u32 rounds.
toF64#
pub fn toF64(value: u32) -> f64Converts value exactly to f64. Every u32 value is below 2^53 and so is exactly
representable.
wrappingToU8#
pub fn wrappingToU8(value: u32) -> u8Converts value to u8 modulo 2^8. Keeps the low 8 bits of the two's complement
form of value and reads them as u8, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToU16#
pub fn wrappingToU16(value: u32) -> u16Converts value to u16 modulo 2^16. Keeps the low 16 bits of the two's complement
form of value and reads them as u16, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToU32#
pub fn wrappingToU32(value: u32) -> u32Returns value unchanged. Use this function when generic wrapping-conversion code
can select u32 as both source and destination.
wrappingToU64#
pub fn wrappingToU64(value: u32) -> u64Converts value to u64 modulo 2^64. Keeps the low 64 bits of the two's complement
form of value and reads them as u64, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToUsize#
pub fn wrappingToUsize(value: u32) -> usizeConverts value to usize modulo 2^usize.BITS. Keeps the low usize.BITS bits of the two's
complement form of value and reads them as usize, so it never traps. Use this function for
deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToI8#
pub fn wrappingToI8(value: u32) -> i8Converts value to i8 modulo 2^8. Keeps the low 8 bits of the two's complement
form of value and reads them as i8, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToI16#
pub fn wrappingToI16(value: u32) -> i16Converts value to i16 modulo 2^16. Keeps the low 16 bits of the two's complement
form of value and reads them as i16, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToI32#
pub fn wrappingToI32(value: u32) -> i32Converts value to i32 modulo 2^32. Keeps the low 32 bits of the two's complement
form of value and reads them as i32, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToI64#
pub fn wrappingToI64(value: u32) -> i64Converts value to i64 modulo 2^64. Keeps the low 64 bits of the two's complement
form of value and reads them as i64, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToIsize#
pub fn wrappingToIsize(value: u32) -> isizeConverts value to isize modulo 2^isize.BITS. Keeps the low isize.BITS bits of the two's
complement form of value and reads them as isize, so it never traps. Use this function for
deliberate truncation, sign extension, or bit reinterpretation, such as reading a signed field.
add#
pub fn add(left: u32, right: u32) -> u32Returns left + right and traps if the result is outside the u32 range. Use this function
when overflow is a program error.
subtract#
pub fn subtract(left: u32, right: u32) -> u32Returns left - right and traps if the result is outside the u32 range. Use this function
when overflow is a program error.
multiply#
pub fn multiply(left: u32, right: u32) -> u32Returns left * right and traps if the result is outside the u32 range. Use this function
when overflow is a program error.
divide#
pub fn divide(left: u32, right: u32) -> u32Returns left / right. Traps if right is zero. Use this function when a zero
divisor is a program error.
remainder#
pub fn remainder(left: u32, right: u32) -> u32Returns the remainder of left / right. Traps if right is zero. Use this function
when a zero divisor is a program error.
bitAnd#
pub fn bitAnd(left: u32, right: u32) -> u32Returns the bitwise AND of left and right.
bitOr#
pub fn bitOr(left: u32, right: u32) -> u32Returns the bitwise OR of left and right.
bitXor#
pub fn bitXor(left: u32, right: u32) -> u32Returns the bitwise exclusive OR of left and right.
bitNot#
pub fn bitNot(value: u32) -> u32Returns value with each bit inverted.
shiftLeft#
pub fn shiftLeft(left: u32, right: u32) -> u32Shifts left bits left by right positions. Traps if right is not less than BITS.
shiftRight#
pub fn shiftRight(left: u32, right: u32) -> u32Shifts left bits right by right positions and inserts zero bits. Traps if right is
not less than BITS.
rotateLeft#
pub fn rotateLeft(left: u32, right: u32) -> u32Rotates the bits of left left by right positions. The count is reduced modulo BITS, so
each count is valid.
rotateRight#
pub fn rotateRight(left: u32, right: u32) -> u32Rotates the bits of left right by right positions. The count is reduced modulo BITS, so
each count is valid.
countOnes#
pub fn countOnes(value: u32) -> u32Returns the number of one bits in value.
leadingZeros#
pub fn leadingZeros(value: u32) -> u32Returns the number of zero bits above the highest one bit of value. Returns BITS for
zero.
trailingZeros#
pub fn trailingZeros(value: u32) -> u32Returns the number of zero bits below the lowest one bit of value. Returns BITS for zero.
wrappingAdd#
pub fn wrappingAdd(left: u32, right: u32) -> u32Returns left + right, wrapped to the u32 range. Use this function for
deliberate modulo arithmetic.
wrappingSubtract#
pub fn wrappingSubtract(left: u32, right: u32) -> u32Returns left - right, wrapped to the u32 range. Use this function for
deliberate modulo arithmetic.
wrappingMultiply#
pub fn wrappingMultiply(left: u32, right: u32) -> u32Returns left * right, wrapped to the u32 range. Use this function for
deliberate modulo arithmetic.
saturatingAdd#
pub fn saturatingAdd(left: u32, right: u32) -> u32Returns left + right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingSubtract#
pub fn saturatingSubtract(left: u32, right: u32) -> u32Returns left - right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingMultiply#
pub fn saturatingMultiply(left: u32, right: u32) -> u32Returns left * right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
multiplyHigh#
pub fn multiplyHigh(left: u32, right: u32) -> u32Returns the high 32 bits of the exact 64-bit product left * right. It never overflows.
When to use#
Use this function with wrappingMultiply, which returns the low 32 bits, when an algorithm
needs the full product, such as multi-word arithmetic, multiply-shift hashing, or division by
a precomputed reciprocal.
checkedAdd#
pub fn checkedAdd(left: u32, right: u32) -> Option<u32>Returns Some with left + right, or None if the result is outside the u32 range.
Use this function when overflow is input data.
checkedSubtract#
pub fn checkedSubtract(left: u32, right: u32) -> Option<u32>Returns Some with left - right, or None if the result is outside the u32 range.
Use this function when overflow is input data.
checkedMultiply#
pub fn checkedMultiply(left: u32, right: u32) -> Option<u32>Returns Some with left * right, or None if the result is outside the u32 range.
Use this function when overflow is input data.
checkedDivide#
pub fn checkedDivide(left: u32, right: u32) -> Option<u32>Returns Some with left / right, or None if right is zero. Use this
function when a zero divisor is input data.
checkedRemainder#
pub fn checkedRemainder(left: u32, right: u32) -> Option<u32>Returns Some with the remainder, or None if right is zero. Use this function
when a zero divisor is input data.
equals#
pub fn equals(left: u32, right: u32) -> boolReturns true if left and right are equal.
notEquals#
pub fn notEquals(left: u32, right: u32) -> boolReturns true if left and right are not equal.
lessThan#
pub fn lessThan(left: u32, right: u32) -> boolReturns true if left is less than right.
lessOrEqual#
pub fn lessOrEqual(left: u32, right: u32) -> boolReturns true if left is less than or equal to right.
greaterThan#
pub fn greaterThan(left: u32, right: u32) -> boolReturns true if left is greater than right.
greaterOrEqual#
pub fn greaterOrEqual(left: u32, right: u32) -> boolReturns true if left is greater than or equal to right.
parse#
pub fn parse(text: string) -> Result<u32, ParseError>Returns the u32 value of the decimal text text, or a ParseError.
Details#
The complete text must be one decimal number. This function does not skip spaces and does not accept digit separators.
Errors#
ParseErrorwith reasonsilk.text.format.NotANumberiftextis empty, has a sign, contains a byte that is not a digit, or has bytes after the number.offsetgives the byte position where the read stopped.ParseErrorwith reasonsilk.text.format.OutOfRangeif the number is outside the range ofu32.
Category: Numbers.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.