Reading bits

Streams (or sections of streams) can be treated as a string of bits, packed either most significant or least significant bits first.

To read from an msb packed stream, use fz_read_bits:


\begin{lstlisting}
/*
fz_read_bits: Read the next n bits from a stream (assumed...
...igned int fz_read_bits(fz_context *ctx, fz_stream *stm, int n);
\end{lstlisting}

Conversely, to read from a lsb packed stream, use fz_read_rbits:


\begin{lstlisting}
/*
fz_read_rbits: Read the next n bits from a stream (assume...
...gned int fz_read_rbits(fz_context *ctx, fz_stream *stm, int n);
\end{lstlisting}
;

Whichever of these is used, reading n bits will return the results in the lowest n bits of the returned value.

After reading bits using these functions, if a return to reading bytewise (or objectwise) is required, then fz_sync_bits must be called.


\begin{lstlisting}
/*
fz_sync_bits: Called after reading bits to tell the strea...
.../
void fz_sync_bits(fz_context *ctx FZ_UNUSED, fz_stream *stm);
\end{lstlisting}

This function skips as many bits as as required to align with a byte boundary.