Padding a Bitstream into Whole Bytes

A bitstream stored as bytes may leave unused bits in its last byte. Its format needs a way to tell the decoder how many bits matter.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a five-bit message
  2. Choose an ending convention
  3. Determine which final bits carry data
  4. Test inputs around the byte boundary

Trace a five-bit message

Holding five significant bits in one byte can leave three pad bits. A decoder that interprets all eight as data may emit extra symbols. See prefix decoding.

Choose an ending convention

A format can store meaningful length, name an end marker, or infer it from other fields. The convention must distinguish padding from real zeros. Sentinel branching illustrates another explicit completion signal.[1][2]

Determine which final bits carry data

A message with thirteen bits occupies two bytes when packed. Three positions in the final physical byte are padding under a simple format. A decoder needs the true length or another unambiguous end marker. See fixed-length codewords.

Useful bits 13
Storage 16 bits
Padding 3 bits
With a recorded bit length, read exactly the first 13 defined positions

Test inputs around the byte boundary

With exactly eight bits there is no partial last byte. With nine there are seven pad positions. Confirm the format's pad-bit rules and what happens for an empty input.

References

  1. Claude E. Shannon, A Mathematical Theory of Communication ↩
  2. David J. C. MacKay, Information Theory, Inference, and Learning Algorithms ↩