Cumulative Shannon code (source code)

= Cumulative Shannon code

Order source probabilities as $p_1\geq\cdots\geq p_N$, put
$$
b_i=\sum_{j<i}p_j,
\qquad
l_i=\lceil-\log_2p_i\rceil,
$$
and take the first $l_i$ binary digits of $b_i$ as the codeword for symbol $i$. Since $b_j-b_i\geq p_i\geq2^{-l_i}$ for $j>i$, two cumulative probabilities cannot lie in the same dyadic interval selected by the earlier codeword. The resulting code is prefix-free.