Types for manipulating numeric primitives at the bit level.
The quark
crate provides traits for accessing parts of numeric primitives and
adds new types to represent numbers using bit counts that aren't included in the
standard library.
Accessing a bit or range of bits in a numeric primitive can be awkward and less
than readable using shifts and masks. Using the BitIndex
trait, these lines
are the same:
let small = big >> 2 & 0x1f;
let small = big.bits(2..7);
The BitMask
trait allows for easily generating a bit mask using just the
length and applying masks:
assert_eq!(u32::mask(8), 0xff);
assert_eq!(0x1234_5678_u32.mask_to(16), 0x5678);
The Signs
trait adds methods for checking the sign bit on unsigned primitives
(and signed ones) and for sign-extending values an arbitrary number of bits:
let signed = unsigned.sign_extend(8);
Because types like i32
, u8
, and usize
are like atoms of data. The quark
crate goes one level down, and quarks are one level down from atoms.