A bit-reader allows for bits level access to a sequence of bytes, allowing bit-level reads that easily cross byte-level
boundaries. You can think of a bit-reader like a long sequence of bits that can be shifted off, providing access to
later bits. Consider:
A bit-reader allows for bits level access to a sequence of bytes, allowing bit-level reads that easily cross byte-level boundaries. You can think of a bit-reader like a long sequence of bits that can be shifted off, providing access to later bits. Consider:
If you wanted the most-significant 4-bits of this byte sequence, you could use a bitmask and a bitwise shifts:
This can be useful for simple encoded data, however, can become unweildly when crossing multiple bytes. Let's say you wanted to get the bits
With bitwise operators on a
Uint8Array
, you'd have to:With a BitReader, you can instead say:
This can be very useful when parsing densely-packed data-structures, especially when they use variable-length encoding.
See
createBitReader