ArticleslgStudy

computer science

Truncated binary encoding

Truncated binary encoding is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Truncated binary encoding rather than just read about it. In short: 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.

Key takeaways

  • Truncated binary encoding belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Truncated binary encoding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Truncated binary encoding from memory before moving on to harder problems.

Reference excerpt

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 < n is the simple binary code for x of length log2(n). Otherwise let k = floor(log2(n)), such that 2k < n < 2k+1and let u = 2k+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.

History Used since at least 1984, phase-in codes, also known as economy codes, are also known as truncated binary encoding.

Example with n = 5 For example, for the alphabet {0, 1, 2, 3, 4}, n = 5 and 22 ≤ n < 23, hence k = 2 and u = 23 − 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 23 − n = 8 − 5 = 3 are unused. In numerical terms, to send a value x, where 0 ≤ x < n, and where there are 2k ≤ n < 2k+1 symbols, there are u = 2k+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 24 − 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 = 23 − 7 = 1:

This last example demonstrates that a leading zero bit does not always indicate a short code; if u < 2k, some long codes will begin with a zero bit.

Simple algorithm Generate the truncated binary encoding for a value x, 0 ≤ x < n, where n > 0 is the size of the alphabet containing x. n need not be a power of two.

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.

On efficiency If n is not a power of two, and k-bit symbols are observed with probability p, then (k + 1)-bit symbols are observed with probability 1 − p. We can calculate the expected number of bits per symbol be as

be=pk+(1−p)(k+1). Raw encoding of the symbol has bu=k+1 bits. Then relative space saving s (see Data compression ratio) of the encoding can be defined as

s=1−bebu=1−pk+(1−p)(k+1)k+1. When simplified, this expression leads to

s=pk+1=pbu. 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 bu decreases.

See also Benford's law Golomb coding

References

Worked examples

Example 1 — a first encounter with Truncated binary encoding

Start with the simplest possible case. Write down what Truncated binary encoding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Truncated binary encoding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Truncated binary encoding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Truncated binary encoding

In research
Truncated binary encoding appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Truncated binary encoding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Truncated binary encoding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Entropy coding, Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Truncated binary encoding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Truncated binary encoding” →

How to study Truncated binary encoding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Truncated binary encoding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Truncated binary encoding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Truncated binary encoding in simple terms?

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.

Why does Truncated binary encoding matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Truncated binary encoding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Truncated binary encoding.

Tags

  • Entropy coding
  • Lossless compression algorithms

Keep exploring