Crate bit_int

Source
Expand description

The bit-int crate is a library for providing arbitrary fixed bit-width integers.

The value is represented by either of the following types:

  • The BitInt type represents a N-bit signed integer. This type is similar to signed _BitInt(n) in C23, or arbitrary fixed bit-width signed integer types (e.g., i7) in Zig.
  • The BitUint type represents a N-bit unsigned integer. This type is similar to unsigned _BitInt(n) in C23, or arbitrary fixed bit-width unsigned integer types (e.g., u7) in Zig.

The largest size of N depends on the size of the underlying type in bits. Therefore, when the underlying type of BitInt is i32, the largest size of N is i32::BITS, and when the underlying type of BitUint is u64, the largest size of N is u64::BITS.

§Examples

§Signed integer type

use bit_int::BitInt;

type Int = BitInt<i8, 7>;

let n = Int::MIN;
assert_eq!(format!("{n}"), "-64");

let n = n.checked_add(106).unwrap();
// Gets the contained value as the underlying type.
assert_eq!(n.get(), 42);

let n = n.checked_add(21).unwrap();
assert_eq!(n.get(), 63);
assert_eq!(n, Int::MAX);

assert_eq!(n.checked_add(22), None);

§Unsigned integer type

use bit_int::BitUint;

type Uint = BitUint<u8, 7>;

let n = Uint::MIN;
assert_eq!(format!("{n}"), "0");

let n = n.checked_add(42).unwrap();
// Gets the contained value as the underlying type.
assert_eq!(n.get(), 42);

let n = n.checked_add(85).unwrap();
assert_eq!(n.get(), 127);
assert_eq!(n, Uint::MAX);

assert_eq!(n.checked_add(86), None);

Structs§

BitInt
BitInt is a type that represents a N-bit signed integer.
BitUint
BitUint is a type that represents a N-bit unsigned integer.

Type Aliases§

BitI8
A specialized BitInt type whose the underlying type is restricted to i8.
BitI16
A specialized BitInt type whose the underlying type is restricted to i16.
BitI32
A specialized BitInt type whose the underlying type is restricted to i32.
BitI64
A specialized BitInt type whose the underlying type is restricted to i64.
BitI128
A specialized BitInt type whose the underlying type is restricted to i128.
BitIsize
A specialized BitInt type whose the underlying type is restricted to isize.
BitU8
A specialized BitUint type whose the underlying type is restricted to u8.
BitU16
A specialized BitUint type whose the underlying type is restricted to u16.
BitU32
A specialized BitUint type whose the underlying type is restricted to u32.
BitU64
A specialized BitUint type whose the underlying type is restricted to u64.
BitU128
A specialized BitUint type whose the underlying type is restricted to u128.
BitUsize
A specialized BitUint type whose the underlying type is restricted to usize.