# Rice Notes

```Rice (Golomb) Code Revision:
1- Iff log2(M) = K where K is an integer, the following are true:
a. Q = S &gt;&gt; K (K-bits right shifted)
b. R = S &amp; (M - 1) (S bitwise ANDed with (M - 1))
c. R can be represented using K bits.
2- Iff log2(M) = K where K is an integer, M-1 is always K ones (if K=4, M-1 = 1111)
3- S (integer) = Q x M +R, where log2(M) = K
4- S (codeword) = [uQ | bR],
a. where bR is binary (00, 01,10,11 (K=2), or 000,001,… (K=3), … etc.)
b. and uQ is unary which means, 0  u0, 1(or b001)  10, 2 (or b010)  u110, 7
(b111)  u11111110 (!!!! )
5- Iff S &lt; M, Q = 0 (unary 0 also) and the remainder is from 0 to M -1
6- R (remainder) is always K-bits, even if S&lt;M
7- There are two conditions that needs to be strictly fulfilled to guarantee optimum
encoding:
a. Number of bits(of the fixed code model need to be compressed) - K &gt; 1 (at least)
b. M must be selected to guarantee minimum Q
8- If the conditions in 7 are not satisfied, then Rice encoder will encode more bits than the
original input
9- We can generalize 7 to say that rice can at least reduce 1 bit iff:
a. #of bits (fixed) – K &gt;= (length(Q) + 1 )
See examples next page !!
Example: try to encode the integer sequence {0, … , 48} using M = 4 (22), M= 8 (23), M = 16(24)
and M = 32 (25) assuming 6 bits and 8 bits representation. Hence compute the reduction in
average if those integer are repeated 10 times in a file.
6-bit
representation
000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
001101
001110
001111
010000
010001
010010
010011
010100
010101
010110
010111
011000
011001
011010
011011
011100
011101
011110
011111
100000
100001
100010
100011
100100
M= 4 (22)
M= 8 (23)
M = 16 (24)
M = 32 (25)
000
001
010
011
1000
1001
1010
1011
11000
11001
11010
11011
111000
111001
111010
111011
1111000
1111001
1111010
1111011
11111000
11111001
11111010
11111011
111111000
111111001
111111010
111111011
1111111000
1111111001
1111111010
1111111011
11111111000
11111111001
11111111010
11111111011
111111111000
0000
0001
0010
0011
0100
0101
0110
0111
10000
10001
10010
10011
10100
10101
10110
10111
110000
110001
110010
110011
110100
110101
110110
110111
1110000
1110001
1110010
1110011
1110100
1110101
1110110
1110111
11110000
11110001
11110010
11110011
11110100
00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
100000
100001
100010
100011
100100
100101
100110
100111
101000
101001
101010
101011
101100
101101
101110
101111
1100000
1100001
1100010
1100011
1100100
000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
001101
001110
001111
010000
010001
010010
010011
010100
010101
010110
010111
011000
011001
011010
011011
011100
011101
011110
011111
1000000
1000001
1000010
1000011
1000100
100101
111111111001
100110
111111111010
100111
111111111011
101000
1111111111000
101001
1111111111001
101010
1111111111010
101011
1111111111011
101100 11111111111000
101101 11111111111001
101110 11111111111010
101111 11111111111011
110000 111111111111000
Uncompressed:
0.6950 
compressed
8-bit
representation
000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
001101
001110
001111
010000
010001
11110101
11110110
11110111
111110000
111110001
111110010
111110011
111110100
111110101
111110110
111110111
1111110000
0.9130
M= 4 (22) M= 8 (23)
000
001
010
011
1000
1001
1010
1011
11000
11001
11010
11011
111000
111001
111010
111011
1111000
1111001
0000
0001
0010
0011
0100
0101
0110
0111
10000
10001
10010
10011
10100
10101
10110
10111
110000
110001
1100101
1100110
1100111
1101000
1101001
1101010
1101011
1101100
1101101
1101110
1101111
11100000
0.9932
M= 16 (24)
00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
100000
100001
1000101
1000110
1000111
1001000
1001001
1001010
1001011
1001100
1001101
1001110
1001111
1010000
0.9453
M= 32 (25)
000000
000001
000010
000011
000100
000101
000110
000111
001000
001001
001010
001011
001100
001101
001110
001111
010000
010001
010010
1111010
010011
1111011
010100
11111000
010101
11111001
010110
11111010
010111
11111011
011000
111111000
011001
111111001
011010
111111010
011011
111111011
011100
1111111000
011101
1111111001
011110
1111111010
011111
1111111011
100000
11111111000
100001
11111111001
100010
11111111010
100011
11111111011
100100
111111111000
100101
111111111001
100110
111111111010
100111
111111111011
101000
1111111111000
101001
1111111111001
101010
1111111111010
101011
1111111111011
101100 11111111111000
101101 11111111111001
101110 11111111111010
101111 11111111111011
110000 111111111111000
110010
110011
110100
110101
110110
110111
1110000
1110001
1110010
1110011
1110100
1110101
1110110
1110111
11110000
11110001
11110010
11110011
11110100
11110101
11110110
11110111
111110000
111110001
111110010
111110011
111110100
111110101
111110110
111110111
100010
100011
100100
100101
100110
100111
101000
101001
101010
101011
101100
101101
101110
101111
1100000
1100001
1100010
1100011
1100100
1100101
1100110
1100111
1101000
1101001
1101010
1101011
1101100
1101101
1101110
1101111
11100000
010010
010011
010100
010101
010110
010111
011000
011001
011010
011011
011100
011101
011110
011111
1000000
1000001
1000010
1000011
1000100
1000101
1000110
1000111
1001000
1001001
1001010
1001011
1001100
1001101
1001110
1001111
1010000
1111110000
Uncompressed:
compressed
0.9267 
1.2174
1.3243
1.26
```