Gamma code

  • Elias gamma code is a universal code encoding positive integers developed by Peter Elias.
  • It is used most commonly when coding integers whose upper-bound cannot be determined beforehand.

Encoding

To code a number x≥1:

  1. Let N=⌊log2 x⌋ be the highest power of 2 it contains, so 2Nx < 2N+1.
  2. Write out N zero bits, then
  3. Append the binary form of x, an N+1-bit binary number.

An equivalent way to express the same process:

  1. Encode N in unary; that is, as N zeroes followed by a one.
  2. Append the remaining N binary digits of x to this representation of N.

To represent a number x, Elias gamma uses 2\lfloor \log _{2}(x)\rfloor +1 bits.[1]:199

The code begins (the implied probability distribution for the code is added for clarity):

NumberBinaryγ EncodingImplied probability
1 = 20 + 0111/2
2 = 21 + 01 00 1 01/8
3 = 21 + 11 10 1 11/8
4 = 22 + 01 0000 1 001/32
5 = 22 + 11 0100 1 011/32
6 = 22 + 21 1000 1 101/32
7 = 22 + 31 1100 1 111/32
8 = 23 + 01 000000 1 0001/128
9 = 23 + 11 001000 1 0011/128
10 = 23 + 21 010000 1 0101/128
11 = 23 + 31 011000 1 0111/128
12 = 23 + 41 100000 1 1001/128
13 = 23 + 51 101000 1 1011/128
14 = 23 + 61 110000 1 1101/128
15 = 23 + 71 111000 1 1111/128
16 = 24 + 01 00000000 1 00001/512
17 = 24 + 11 00010000 1 00011/512

Decoding

To decode an Elias gamma-coded integer:

  1. Read and count 0s from the stream until you reach the first 1. Call this count of zeroes N.
  2. Considering the one that was reached to be the first digit of the integer, with a value of 2N, read the remaining N digits of the integer.

Uses

Gamma coding is used in applications where the largest encoded value is not known ahead of time, or to compress data in which small values are much more frequent than large values.

Gamma coding is a building block in the Elias delta code.

Generalizations

Gamma coding does not code zero or negative integers. One way of handling zero is to add 1 before coding and then subtract 1 after decoding. Another way is to prefix each nonzero code with a 1 and then code zero as a single 0.

One way to code all integers is to set up a bijection, mapping integers (0, −1, 1, −2, 2, −3, 3, …) to (1, 2, 3, 4, 5, 6, 7, …) before coding. In software, this is most easily done by mapping non-negative inputs to odd outputs, and negative inputs to even outputs, so the least-significant bit becomes an inverted sign bit:
{ x ↦ 2 x + 1 w h e n   x ≥ 0 x ↦ − 2 x w h e n   x < 0 {\displaystyle {

{x2x+1x2xamp;when x0amp;when xlt;0
}} {\begin{cases}x\mapsto 2x+1&amp;{\mathrm  {when~}}x\geq 0\\x\mapsto -2x&amp;{\mathrm  {when~}}x&lt;0\\\end{cases}}

Exponential-Golomb coding generalizes the gamma code to integers with a “flatter” power-law distribution, just as Golomb coding generalizes the unary code. It involves dividing the number by a positive divisor, commonly a power of 2, writing the gamma code for one more than the quotient, and writing out the remainder in an ordinary binary code.

Learn from wikipedia.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值