Crate bitty [] [src]

bitty - helps a bit

bitty contains functions to extract bits from, and put back into integer types.


Include bitty in your Cargo.toml:

bitty = "1.0"

Use cases

Extracting individual bits:

let five_as_bits: Vec<bool> = 5u8.as_bits();
// Expected: 1 0 1 0 0 0 0 0
assert_eq!(five_as_bits, vec![true, false, true, false, false, false, false, false]);

Putting bits back into an u8:

let five_from_bits: u8 = u8::from_bits(&5u8.as_bits());
assert_eq!(5, five_from_bits);

Creating an integer from some bits:

// Note that the vector does not have to contain 64 bits.
// Missing bits default to 0.
let one_from_bits: u64 = u64::from_bits(&vec![true]);
assert_eq!(1, one_from_bits);


pub use traits::*;