#bit-fields #read-write #bit #lsb #binary

bitorder

Library to read and write bits in lsb or msb order

2 releases

0.0.2 Jan 24, 2022
0.0.1 Jan 20, 2022

#2363 in Encoding

MIT license

13KB
307 lines

bitorder

A library to read and write bits in lsb or msb order. Modeled after byteorder but for bits. Goal is to be simple and fast.

Documentation

https://docs.rs/bitorder


lib.rs:

A small library to read and write bits in Lsb or Msb order.

Example

use bitorder::container::{BitReader, BitWriter};
use bitorder::{Msb0, BitOrder};

let mut data = vec![0u8; 2];
let mut b = BitWriter::<Msb0>::new(&mut data);
b.write_bits(1, 1u8);
b.write_bits(2, 2u8);
b.write_bits(3, 3u8);
b.write_bits(4, 4u8);
b.write_bits(5, 5u8);

assert_eq!(&data, b"\xCD\x0A"); // http://mjfrazer.org/mjfrazer/bitfields/

let mut b = BitReader::<Msb0>::new(&data);
assert_eq!(b.read_bits::<u8>(1), 1u8);
assert_eq!(b.read_bits::<u8>(2), 2u8);
assert_eq!(b.read_bits::<u8>(3), 3u8);
assert_eq!(b.read_bits::<u8>(4), 4u8);
assert_eq!(b.read_bits::<u8>(5), 5u8);

No runtime deps