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. If n is a power of two, then the coded value for 0 ≤ x 2 (n). Otherwise let k = floor(log 2 (n)), such that 2 k k+1 and let u = 2 k+1 − n. Truncated binary encoding assigns the first u symbols codewords of length k and then assigns the remaining n − u symbols the last n − u 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.
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. If n is a power of two, then the coded value for 0 ≤ x 2 (n). Otherwise let k = floor(log 2 (n)), such that 2 k k+1 and let u = 2 k+1 − n. Truncated binary encoding assigns the first u symbols codewords of length k and then assigns the remaining n − u symbols the last n − u 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. ==Example with n = 5== For example, for the alphabet {0, 1, 2, 3, 4}, n = 5 and 2 2 ≤ n 3 , hence k = 2 and u = 2 3 − 5 = 3. Truncated binary encoding assigns the first u symbols the codewords 00, 01, and 10, all of length 2, then assigns the last n − u symbols the codewords 110 and 111, the last two codewords of length 3. For example, if n is 5, plain binary encoding and truncated binary encoding allocates the following codewords. Digits shown struck are not transmitted in truncated binary. It takes 3 bits to encode n using straightforward binary encoding, hence 2 3 − n = 8 − 5 = 3 are unused. In numerical terms, to send a value x, where 0 ≤ x k ≤ n k+1 symbols, there are u = 2 k+1 − n unused entries when the alphabet size is rounded up to the nearest power of two. The process to encode the number x in truncated binary is: if x is less than u, encode it in k binary bits; if x is greater than or equal to u, encode the value x + u in k + 1 binary bits. ==Example with n = 10== Another example, encoding an alphabet of size 10 (between 0 and 9) requires 4 bits, but there are 2 4 − 10 = 6 unused codes, so input values less than 6 have the first bit discarded, while input values greater than or equal to 6 are offset by 6 to the end of the binary space. (Unused patterns are not shown in this table.) To decode, read the first k bits. If they encode a value less than u, decoding is complete. Otherwise, read an additional bit and subtract u from the result. ==Example with n = 7== Here is a more extreme case: with n = 7 the next power of 2 is 8, so k = 2 and u = 2 3 − 7 = 1: This last example demonstrates that a leading zero bit does not always indicate a short code; if u k , some long codes will begin with a zero bit. string TruncatedBinary (int x, int n) { // Set k = floor(log2(n)), i.e., k such that 2^k 1) { k++; t >>= 1; } // Set u to the number of unused codewords = 2^(k+1) - n. int u = (1 The routine Binary is expository; usually just the rightmost len bits of the variable x are desired. Here we simply output the binary code for x using len bits, padding with high-order 0s if necessary. string Binary (int x, int len) { string s = ""; while (x != 0) { if (even(x)) s = '0' + s; else s = '1' + s; x >>= 1; } while (s.Length b_e = p k + (1 - p) (k + 1). Raw encoding of the symbol has b_u = k + 1 bits. Then relative space saving s (see Data compression ratio) of the encoding can be defined as s = 1 - \frac{b_e}{b_u} = 1 - \frac{p k + (1 - p) (k + 1)}{k + 1}. When simplified, this expression leads to s = \frac{p}{k + 1} = \frac{p}{b_u}. This indicates that relative efficiency of truncated binary encoding increases as probability p of k-bit symbols increases, and the raw-encoding symbol bit-length b_u decreases. ==See also== Benford's law Golomb coding ==References==
References
No reference rows published for this article.
Cite this article
MLA
"Truncated binary encoding." encyclopegia.org, 2026, https://encyclopegia.org/wiki/truncated-binary-encoding. Accessed 24 November 2026.
Chicago
"Truncated binary encoding." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/truncated-binary-encoding.
APA
encyclopegia.org. (2026). Truncated binary encoding. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/truncated-binary-encoding
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON