#bits #bit #integer #bit-manipulation

bitty

Extract bits from, and put back into integer types

1 stable release

Uses old Rust 2015

1.0.0 Nov 28, 2017

#5 in #bitwise

MIT license

8KB
66 lines

bitty


lib.rs:

bitty - helps a bit

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

Usage

Include bitty in your Cargo.toml:

[dependencies]
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);

No runtime deps