Skip to main content

Module bits

Module bits 

Source
Expand description

Big-endian, MSB-first bit reader/writer for dense sub-byte wire fields.

DVB physical-layer signalling (e.g. the EN 302 755 §7.2 L1-pre / L1-post tables) packs many fields that are not byte-aligned — 1-bit flags, 3-bit codes, 18-bit sizes — back-to-back with no padding. BitReader and BitWriter walk such a stream a field at a time, most-significant bit first within each field and within each byte, which is the bit order used throughout the DVB/MPEG specifications.

Both sides are symmetric: bits written by BitWriter::write_bits read back identically through BitReader::read_bits. The writer sets and clears each target bit, so the destination buffer need not be pre-zeroed.

use dvb_common::bits::{BitReader, BitWriter};

let mut buf = [0u8; 2];
let mut w = BitWriter::new(&mut buf);
w.write_bits(0b101, 3).unwrap();   // 3-bit field
w.write_bits(0x1FF, 9).unwrap();   // 9-bit field — crosses the byte boundary
assert_eq!(w.bits_written(), 12);

let mut r = BitReader::new(&buf);
assert_eq!(r.read_bits(3).unwrap(), 0b101);
assert_eq!(r.read_bits(9).unwrap(), 0x1FF);

Structs§

BitReader
Reads fields MSB-first from a borrowed byte slice.
BitWriter
Writes fields MSB-first into a borrowed mutable byte slice.

Enums§

BitError
Error from a BitReader / BitWriter operation.