silk/u64
Sixty-four-bit unsigned integers for wide masks, counters, hashes, and exact interchange values.
When to use
Use u64 when a stable unsigned 64-bit representation matters. It is the representation used
for hash output; use usize instead for target-sized collection lengths and indices.
Details
Ordinary arithmetic, narrowing conversions, division by zero, and invalid shift counts trap.
checked* returns Option, wrapping* computes modulo 2^64, and saturating* clamps at
MIN or MAX. Right shift inserts zero bits.
Decimal parse consumes the complete input and separates malformed text from range overflow.
toText allocates owned text. Conversion to a floating type may round large values.
Gotchas
A u64 can represent values that neither i64 nor every target's usize can hold; use a
checked conversion when that boundary is controlled by input.
Examples
Read the high half of a 64-bit field
import silk.u64 as u64
pub fn main() -> i32 {
let high = u64.shiftRight(0x0000002A00000000, 32)
return u64.toI32(high)
}Import as u64 with import silk.u64.
Public declarations: 57.
MAX
pub const MAX: u64The largest u64 value.
MIN
pub const MIN: u64The smallest u64 value.
BITS
pub const BITS: u32The fixed width of u64, in bits.
toU8
pub fn toU8(value: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> u32Converts value to u32. Traps if value is outside the u32 range. Use
this function when an out-of-range value is a program error.
checkedToU32
pub fn checkedToU32(value: u64) -> Option<u32>Converts value to u32, or returns None if value is outside the u32
range. Use this function when an out-of-range value is input data.
toU64
pub fn toU64(value: u64) -> u64Returns value unchanged as u64. Use this function when generic conversion code
can select u64 as both source and destination.
checkedToU64
pub fn checkedToU64(value: u64) -> Option<u64>Returns Some with value unchanged as u64. Use this function when generic
checked-conversion code can select the same source and destination type.
toUsize
pub fn toUsize(value: u64) -> usizeConverts value to usize. Traps if value is outside the usize range. Use
this function when an out-of-range value is a program error.
checkedToUsize
pub fn checkedToUsize(value: u64) -> Option<usize>Converts value to usize, or returns None if value is outside the usize
range. Use this function when an out-of-range value is input data.
toI8
pub fn toI8(value: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> 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: u64) -> i64Converts value to i64. Traps if value is outside the i64 range. Use
this function when an out-of-range value is a program error.
checkedToI64
pub fn checkedToI64(value: u64) -> Option<i64>Converts value to i64, or returns None if value is outside the i64
range. Use this function when an out-of-range value is input data.
toIsize
pub fn toIsize(value: u64) -> 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: u64) -> 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: u64) -> f32Converts value to the nearest f32 value, with ties to even.
toF64
pub fn toF64(value: u64) -> f64Converts value to the nearest f64 value, with ties to even.
add
pub fn add(left: u64, right: u64) -> u64Returns left + right and traps if the result is outside the u64 range. Use this function
when overflow is a program error.
subtract
pub fn subtract(left: u64, right: u64) -> u64Returns left - right and traps if the result is outside the u64 range. Use this function
when overflow is a program error.
multiply
pub fn multiply(left: u64, right: u64) -> u64Returns left * right and traps if the result is outside the u64 range. Use this function
when overflow is a program error.
divide
pub fn divide(left: u64, right: u64) -> u64Returns left / right. Traps if right is zero. Use this function when a zero
divisor is a program error.
remainder
pub fn remainder(left: u64, right: u64) -> u64Returns 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: u64, right: u64) -> u64Returns the bitwise AND of left and right.
bitOr
pub fn bitOr(left: u64, right: u64) -> u64Returns the bitwise OR of left and right.
bitXor
pub fn bitXor(left: u64, right: u64) -> u64Returns the bitwise exclusive OR of left and right.
bitNot
pub fn bitNot(value: u64) -> u64Returns value with each bit inverted.
shiftLeft
pub fn shiftLeft(left: u64, right: u64) -> u64Shifts left bits left by right positions. Traps if right is not less than BITS.
shiftRight
pub fn shiftRight(left: u64, right: u64) -> u64Shifts left bits right by right positions and inserts zero bits. Traps if right is
not less than BITS.
rotateLeft
pub fn rotateLeft(left: u64, right: u64) -> u64Rotates the bits of left left by right positions.
rotateRight
pub fn rotateRight(left: u64, right: u64) -> u64Rotates the bits of left right by right positions.
wrappingAdd
pub fn wrappingAdd(left: u64, right: u64) -> u64Returns left + right, wrapped to the u64 range. Use this function for
deliberate modulo arithmetic.
wrappingSubtract
pub fn wrappingSubtract(left: u64, right: u64) -> u64Returns left - right, wrapped to the u64 range. Use this function for
deliberate modulo arithmetic.
wrappingMultiply
pub fn wrappingMultiply(left: u64, right: u64) -> u64Returns left * right, wrapped to the u64 range. Use this function for
deliberate modulo arithmetic.
saturatingAdd
pub fn saturatingAdd(left: u64, right: u64) -> u64Returns left + right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingSubtract
pub fn saturatingSubtract(left: u64, right: u64) -> u64Returns left - right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingMultiply
pub fn saturatingMultiply(left: u64, right: u64) -> u64Returns left * right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
checkedAdd
pub fn checkedAdd(left: u64, right: u64) -> Option<u64>Returns Some with left + right, or None if the result is outside the u64 range.
Use this function when overflow is input data.
checkedSubtract
pub fn checkedSubtract(left: u64, right: u64) -> Option<u64>Returns Some with left - right, or None if the result is outside the u64 range.
Use this function when overflow is input data.
checkedMultiply
pub fn checkedMultiply(left: u64, right: u64) -> Option<u64>Returns Some with left * right, or None if the result is outside the u64 range.
Use this function when overflow is input data.
checkedDivide
pub fn checkedDivide(left: u64, right: u64) -> Option<u64>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: u64, right: u64) -> Option<u64>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: u64, right: u64) -> boolReturns true when left and right are equal.
notEquals
pub fn notEquals(left: u64, right: u64) -> boolReturns true when left and right are not equal.
lessThan
pub fn lessThan(left: u64, right: u64) -> boolReturns true when left is less than right.
lessOrEqual
pub fn lessOrEqual(left: u64, right: u64) -> boolReturns true when left is less than or equal to right.
greaterThan
pub fn greaterThan(left: u64, right: u64) -> boolReturns true when left is greater than right.
greaterOrEqual
pub fn greaterOrEqual(left: u64, right: u64) -> boolReturns true when left is greater than or equal to right.
toText
pub effect fn toText(value: u64) -> String ! OutOfMemoryError ? &mut AllocatorRenders the value as base-10 text in new owned storage. Allocation uses the required
Allocator and can fail with OutOfMemoryError.
parse
pub fn parse(text: string) -> silk/result.Result<u64, silk/format.ParseError>Reads the complete text as an unsigned decimal u64.
Details
A failure contains silk.format.NotANumber for empty text, a sign, a non-digit, or trailing
bytes. It contains silk.format.OutOfRange if the number is outside the u64 range.