Rice Codes: A Unary Quotient and a Fixed-Width Remainder
2026-11-18

Rice Codes: A Unary Quotient and a Fixed-Width Remainder

Rice Codes represent a nonnegative integer as two parts: a quotient written in unary and a remainder written in a fixed number of binary bits. The parameter tells both sides where that split occurs. This is different from using a unary prefix to describe the binary length of the entire value. Reading a concrete value through division, encoding and reconstruction makes the parameter's role visible.

The parameter fixes a power-of-two divisor

Let the parameter be k. Divide an unsigned value u by two raised to k, obtaining an integer quotient q and a remainder r. The reconstruction rule is u equals q times two raised to k, plus r. The remainder lies between zero and one less than the divisor, so exactly k binary bits can represent it, including leading zeroes where necessary.

Use a constructed example with u equal to nineteen and k equal to two. The divisor is four, the quotient is four and the remainder is three. The arithmetic check is nineteen equals four times four plus three. Increasing k does not change the value being represented; it changes how much of that value appears in the quotient and how much appears in the remainder.

Write the quotient with a stated unary polarity

The convention used here, consistent with the referenced FLAC specification, writes q zero bits followed by a one. Then it appends the k-bit binary remainder. For nineteen with k equal to two, the quotient four becomes 00001 and the remainder three becomes 11. The complete codeword is 0000111, with seven bits.

The stop bit is part of the unary representation, while the remainder has its agreed fixed width. A printed separator between 00001 and 11 is only explanatory punctuation. Another unary polarity would require a correspondingly different decoder; it should not be silently mixed with this example. The figure colors the two parts so their different jobs remain clear.

Rice u19,k2 splits q4,r3 into unary00001 and fixed2-bit11.
Explicit zeroes-then-one unary convention; parameter is already known to the decoder. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

Decoding needs the parameter before reading the remainder

A decoder counts four zeroes before the first one, giving quotient four. It then reads exactly two further bits because k is two, obtaining remainder three. Recombining them returns nineteen. The quotient does not tell the decoder how many remainder bits to read: that information comes from the shared parameter, typically provided by the surrounding format.

Zero is also a valid unsigned input. With k equal to two, its quotient and remainder are both zero, giving the three-bit codeword 100. With k equal to zero, the remainder occupies no bits and the representation is entirely unary. These boundary cases help distinguish the rule from positive-integer codes that cannot represent zero without an additional mapping.

Changing the split trades unary length against remainder width

The code length is q plus one plus k. For nineteen, parameter one gives quotient nine and an eleven-bit codeword. Parameter two uses seven bits. Parameter three gives quotient two and a six-bit codeword; parameter four gives quotient one and also uses six bits. These are arithmetic comparisons for a single synthetic value, not evidence that one parameter is best for an entire data stream.

For a collection containing many small values, a large fixed remainder width may waste bits repeatedly. A very small parameter can instead produce long unary quotients for occasional large values. Choosing a parameter therefore depends on the values being coded and the cost of signaling the choice. Looking only at the largest value does not generally minimize the complete encoded size.

Foru19,k1/2/3/4 yield q9/4/2/1 and lengths11/7/6/6.
Single-value arithmetic only; parameter signaling and whole-stream distribution remain separate. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

Signed residual folding is a separate reversible step

Rice coding in its elementary form accepts nonnegative integers. FLAC uses a folded representation for signed residuals: a nonnegative signed value maps to twice its value, while a negative value maps to minus twice its value minus one. For example, minus three maps to five and positive three maps to six. After Rice decoding, the inverse fold recovers the signed residual.

The fold does not predict the original audio sample and does not itself eliminate information. It assigns nearby nonnegative values to small positive and negative residuals so that an unsigned code can be applied. Prediction, residual formation, signed mapping and Rice representation are different stages. The article's nineteen example is already unsigned and is not presented as an original audio sample.

A codec supplies partitions, exceptions and framing

The FLAC specification describes partitioned Rice parameters and escaped partitions, while the original bitwriter implementation shows how quotient and low bits enter a real output buffer. Our bare codeword omits those surrounding fields. It therefore cannot be compared directly with a complete FLAC frame size or treated as a universal byte serialization.

The site's coding principles collection and technical features provide context for distinguishing a value-level code from an entire compressed format. A useful explanation always states the input domain, parameter, unary polarity and remainder width before interpreting a string of bits.

Primary references: RFC 9639 residual and Rice coding sections and Xiph's original FLAC bitwriter source. Both figures are original AI-assisted diagrams of synthetic integer arithmetic, without compression benchmarks.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我