Expand description
This crate provides Biterator
, a type for iterating over individual bits
in a stream of bytes.
§Examples
use biterator::{Biterator, Bit::*};
let bytes = [0b00001111, 0b10101011];
let bits: Vec<_> = Biterator::new(&bytes).collect();
assert_eq!(
bits,
vec![
Zero, Zero, Zero, Zero, One, One, One, One,
One, Zero, One, Zero, One, Zero, One, One,
]
);
Use it to find which bits are set in a stream:
use biterator::{Biterator, Bit, BiteratorExt};
let bytes = [0b00110101];
let set_bits: Vec<_> = bytes
.iter()
.bits()
.enumerate()
.filter(|(_, bit)| bit.is_one())
.collect();
assert_eq!(
set_bits,
vec![(2, Bit::One), (3, Bit::One), (5, Bit::One), (7, Bit::One)]
);
Count the number of bits that are 0 in a buffer:
use biterator::BiteratorExt;
// There are 10 zeros here
let buf = [0b00110011, 0b11001111, 0b01010101];
let zero_bit_count = buf.iter().bits().filter(|&b| b.is_zero()).count();
assert_eq!(zero_bit_count, 10);
Structs§
- Biterator
- An iterator over individual bits in a byte stream. It is generic over
I
, a type which implements an iterator over a type which can be borrowed asu8
, which represents the source of bytes for theBiterator
.
Enums§
- Bit
- A single bit.
Traits§
- Biterator
Ext - An extension trait which allows iterators to easily convert themselves into
Biterator
s in method chains.