Truncated Binary Encoding

Truncated binary encoding is an entropy encoding typically used for uniform probability distributions with a finite alphabet. It is parameterized by an alphabet with total size of number n. It is a slightly more general form of binary encoding when n is not a power of two.

Let n = 2k+b, for 0 ≤ b ≤ 2k. If n is a power of 2, you may choose two possible values of k; both produce the same coded values, which are identical to a simple binary code.

Truncated binary encoding assigns the first 2kb symbols codewords of length k and then assigns the remaining 2b symbols the last 2b codewords of length k+1. Because all the codewords of length k+1 consist of an unassigned codeword of length k with a "0" or "1" appended, the resulting code is a prefix code.

Read more about Truncated Binary Encoding:  Example With n = 5, Example With n = 10, Example With n = 7