silk.num.i8.
Eight-bit signed integers for compact values, byte-level formats, and narrow arithmetic.
When to use#
Use i8 when an external format or data structure requires exactly eight signed bits. Prefer a
wider integer for general arithmetic: the small range makes accidental overflow easy.
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^8.The
saturatingform, such assaturatingAdd, returnsMINorMAXon overflow.
Division and remainder by zero trap, and so does MIN divided by -1. checkedDivide and
checkedRemainder return None for these inputs. 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 signed right shift copies the sign bit into the new high 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 is exact.
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 in its two's
complement form. For zero, both zero counts return BITS.
parse reads decimal text. A leading - is valid, and a leading + 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.
Gotchas#
MIN has no positive i8 counterpart, so ordinary negate traps for that one value.
Examples#
Select the boundary policy for negation#
import silk.num.i8
pub fn main() -> i32 {
if i8.wrappingNegate(i8.MIN) != i8.MIN {
return 1
}
if i8.saturatingNegate(i8.MIN) != i8.MAX {
return 1
}
return 0
}Parse decimal text and reject invalid text#
import silk.text.format { ParseError }
import silk.core.result { Result }
import silk.num.i8
pub fn main() -> i32 {
let parsed = i8.parse("42") |> Result<i8, ParseError>.unwrapOr(0)
let invalid = i8.parse("4x2") |> Result<i8, ParseError>.unwrapOr(0)
if parsed != 42 || invalid != 0 {
return 1
}
return 0
}Import as i8 with import silk.num.i8.
Public declarations: 73.
MAX#
pub const MAX: i8The largest i8 value.
MIN#
pub const MIN: i8The smallest i8 value.
BITS#
pub const BITS: u32The fixed width of i8, in bits.
negate#
pub fn negate(value: i8) -> i8Returns the arithmetic negation of value and traps when value is MIN. Use
this function when that boundary is a program error.
wrappingNegate#
pub fn wrappingNegate(value: i8) -> i8Returns the arithmetic negation of value, wrapped to the i8 range. MIN
stays MIN. Use this function for deliberate modulo arithmetic.
saturatingNegate#
pub fn saturatingNegate(value: i8) -> i8Returns the arithmetic negation of value, clamped to the i8 range. MIN
becomes MAX. Use this function when the positive boundary is required.
toU8#
pub fn toU8(value: i8) -> 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: i8) -> 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: i8) -> 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: i8) -> 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: i8) -> 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: i8) -> 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: i8) -> u64Converts value to u64. Traps if value is outside the u64 range. Use
this function when an out-of-range value is a program error.
checkedToU64#
pub fn checkedToU64(value: i8) -> Option<u64>Converts value to u64, or returns None if value is outside the u64
range. Use this function when an out-of-range value is input data.
toUsize#
pub fn toUsize(value: i8) -> 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: i8) -> 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: i8) -> i8Returns value unchanged as i8. Use this function when generic conversion code
can select i8 as both source and destination.
checkedToI8#
pub fn checkedToI8(value: i8) -> Option<i8>Returns Some with value unchanged as i8. Use this function when generic
checked-conversion code can select the same source and destination type.
toI16#
pub fn toI16(value: i8) -> i16Converts value exactly to i16. Every i8 value is representable.
checkedToI16#
pub fn checkedToI16(value: i8) -> Option<i16>Converts value exactly to i16 and returns Some. Every i8 value is
representable.
toI32#
pub fn toI32(value: i8) -> i32Converts value exactly to i32. Every i8 value is representable.
checkedToI32#
pub fn checkedToI32(value: i8) -> Option<i32>Converts value exactly to i32 and returns Some. Every i8 value is
representable.
toI64#
pub fn toI64(value: i8) -> i64Converts value exactly to i64. Every i8 value is representable.
checkedToI64#
pub fn checkedToI64(value: i8) -> Option<i64>Converts value exactly to i64 and returns Some. Every i8 value is
representable.
toIsize#
pub fn toIsize(value: i8) -> isizeConverts value exactly to isize. Every i8 value is representable.
checkedToIsize#
pub fn checkedToIsize(value: i8) -> Option<isize>Converts value exactly to isize and returns Some. Every i8 value is
representable.
toF32#
pub fn toF32(value: i8) -> f32Converts value exactly to f32. Every i8 magnitude is below 2^24 and so is exactly
representable.
toF64#
pub fn toF64(value: i8) -> f64Converts value exactly to f64. Every i8 magnitude is below 2^53 and so is exactly
representable.
wrappingToU8#
pub fn wrappingToU8(value: i8) -> 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: i8) -> 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: i8) -> u32Converts value to u32 modulo 2^32. Keeps the low 32 bits of the two's complement
form of value and reads them as u32, so it never traps. Use this function for deliberate
truncation, sign extension, or bit reinterpretation, such as reading a signed field.
wrappingToU64#
pub fn wrappingToU64(value: i8) -> 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: i8) -> 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: i8) -> i8Returns value unchanged. Use this function when generic wrapping-conversion code
can select i8 as both source and destination.
wrappingToI16#
pub fn wrappingToI16(value: i8) -> 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: i8) -> 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: i8) -> 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: i8) -> 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: i8, right: i8) -> i8Returns left + right and traps if the result is outside the i8 range. Use this function
when overflow is a program error.
subtract#
pub fn subtract(left: i8, right: i8) -> i8Returns left - right and traps if the result is outside the i8 range. Use this function
when overflow is a program error.
multiply#
pub fn multiply(left: i8, right: i8) -> i8Returns left * right and traps if the result is outside the i8 range. Use this function
when overflow is a program error.
divide#
pub fn divide(left: i8, right: i8) -> i8Returns left / right, rounded toward zero. Traps if right is zero or MIN is
divided by -1. Use this function when an invalid quotient is a program error.
remainder#
pub fn remainder(left: i8, right: i8) -> i8Returns the remainder with the sign of left. Traps if right is zero or MIN is
divided by -1. Use this function when invalid division is a program error.
bitAnd#
pub fn bitAnd(left: i8, right: i8) -> i8Returns the bitwise AND of left and right.
bitOr#
pub fn bitOr(left: i8, right: i8) -> i8Returns the bitwise OR of left and right.
bitXor#
pub fn bitXor(left: i8, right: i8) -> i8Returns the bitwise exclusive OR of left and right.
bitNot#
pub fn bitNot(value: i8) -> i8Returns value with each bit inverted.
shiftLeft#
pub fn shiftLeft(left: i8, right: i8) -> i8Shifts left bits left by right positions. Traps if right is negative or not less than
BITS.
shiftRight#
pub fn shiftRight(left: i8, right: i8) -> i8Shifts left bits right by right positions and preserves its sign. Traps if right is
negative or not less than BITS.
rotateLeft#
pub fn rotateLeft(left: i8, right: i8) -> i8Rotates the bits of left left by right positions. The count is reduced modulo BITS, so
each count is valid.
rotateRight#
pub fn rotateRight(left: i8, right: i8) -> i8Rotates the bits of left right by right positions. The count is reduced modulo BITS, so
each count is valid.
countOnes#
pub fn countOnes(value: i8) -> u32Returns the number of one bits in the two's complement form of value.
leadingZeros#
pub fn leadingZeros(value: i8) -> u32Returns the number of zero bits above the highest one bit of the two's complement form of
value. Returns BITS for zero and 0 for a negative value.
trailingZeros#
pub fn trailingZeros(value: i8) -> u32Returns the number of zero bits below the lowest one bit of value. Returns BITS for zero.
wrappingAdd#
pub fn wrappingAdd(left: i8, right: i8) -> i8Returns left + right, wrapped to the i8 range. Use this function for
deliberate modulo arithmetic.
wrappingSubtract#
pub fn wrappingSubtract(left: i8, right: i8) -> i8Returns left - right, wrapped to the i8 range. Use this function for
deliberate modulo arithmetic.
wrappingMultiply#
pub fn wrappingMultiply(left: i8, right: i8) -> i8Returns left * right, wrapped to the i8 range. Use this function for
deliberate modulo arithmetic.
saturatingAdd#
pub fn saturatingAdd(left: i8, right: i8) -> i8Returns left + right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingSubtract#
pub fn saturatingSubtract(left: i8, right: i8) -> i8Returns left - right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
saturatingMultiply#
pub fn saturatingMultiply(left: i8, right: i8) -> i8Returns left * right, clamped to MIN or MAX. Use this function when a
boundary value is the required overflow result.
multiplyHigh#
pub fn multiplyHigh(left: i8, right: i8) -> i8Returns the high 8 bits of the exact 16-bit product left * right. It never overflows.
When to use#
Use this function with wrappingMultiply, which returns the low 8 bits, when an algorithm
needs the full product, such as multi-word arithmetic, multiply-shift hashing, or division by
a precomputed reciprocal.
Details#
The product is signed, so the high half carries its sign: multiplyHigh(-1, 1) is -1. The
full product is the high half times 2^8 plus the low half read as unsigned bits.
checkedAdd#
pub fn checkedAdd(left: i8, right: i8) -> Option<i8>Returns Some with left + right, or None if the result is outside the i8 range.
Use this function when overflow is input data.
checkedSubtract#
pub fn checkedSubtract(left: i8, right: i8) -> Option<i8>Returns Some with left - right, or None if the result is outside the i8 range.
Use this function when overflow is input data.
checkedMultiply#
pub fn checkedMultiply(left: i8, right: i8) -> Option<i8>Returns Some with left * right, or None if the result is outside the i8 range.
Use this function when overflow is input data.
checkedDivide#
pub fn checkedDivide(left: i8, right: i8) -> Option<i8>Returns Some with left / right, or None if right is zero or MIN is
divided by -1. Use this function when an invalid quotient is input data.
checkedRemainder#
pub fn checkedRemainder(left: i8, right: i8) -> Option<i8>Returns Some with the remainder, or None if right is zero or MIN is
divided by -1. Use this function when invalid division is input data.
equals#
pub fn equals(left: i8, right: i8) -> boolReturns true if left and right are equal.
notEquals#
pub fn notEquals(left: i8, right: i8) -> boolReturns true if left and right are not equal.
lessThan#
pub fn lessThan(left: i8, right: i8) -> boolReturns true if left is less than right.
lessOrEqual#
pub fn lessOrEqual(left: i8, right: i8) -> boolReturns true if left is less than or equal to right.
greaterThan#
pub fn greaterThan(left: i8, right: i8) -> boolReturns true if left is greater than right.
greaterOrEqual#
pub fn greaterOrEqual(left: i8, right: i8) -> boolReturns true if left is greater than or equal to right.
parse#
pub fn parse(text: string) -> Result<i8, ParseError>Returns the i8 value of the decimal text text, or a ParseError.
Details#
The complete text must be one decimal number, with an optional leading -. This function does
not skip spaces and does not accept digit separators.
Errors#
ParseErrorwith reasonsilk.text.format.NotANumberiftextis empty, starts with+, 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 ofi8.
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.