BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 36 definitions for Gamma.  Also try: Elias coding.

Elias gamma coding

Print-Friendly
About 3 pages (852 words)

Bookmark and Share Questions on this topic? Just ask!

Elias gamma code is a universal code encoding positive integers. It is used most commonly when coding integers whose upper-bound cannot be determined beforehand. To code a number:

  1. Write it in binary.
  2. Subtract 1 from the number of bits written in step 1 and prepend that many zeros.

An equivalent way to express the same process:

  1. Separate the integer into the highest power of 2 it contains (2N) and the remaining N binary digits of the integer.
  2. Encode N in unary; that is, as N zeroes followed by a one.
  3. Append the remaining N binary digits to this representation of N.

The code begins:

 1 = 20 + 0 = 1
 2 = 21 + 0 = 010
 3 = 21 + 1 = 011
 4 = 22 + 0 = 00100
 5 = 22 + 1 = 00101
 6 = 22 + 2 = 00110
 7 = 22 + 3 = 00111
 8 = 23 + 0 = 0001000
 9 = 23 + 1 = 0001001
10 = 23 + 2 = 0001010
11 = 23 + 3 = 0001011
12 = 23 + 4 = 0001100
13 = 23 + 5 = 0001101
14 = 23 + 6 = 0001110
15 = 23 + 7 = 0001111
16 = 24 + 0 = 000010000
17 = 24 + 1 = 000010001

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.

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.

Contents

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.

Example code

Encode <source lang="c"> void eliasGammaEncode(char* source, char* dest) {

    IntReader intreader(source);
    BitWriter bitwriter(dest);
    while(intreader.hasLeft())     
    {
     int num = intreader.getInt();
     int l = log2(num);
     for (int a=0; a < l; a++)
     {       
         bitwriter.putBit(false); //put 0s to indicate how much bits that will follow
     }
     bitwriter.putBit(true);//mark the end of the 0s
     for (int a=0; a < l; a++) //Write the bits as plain binary
     {
                 if (num & 1 << a)
                    bitwriter.putBit(true);
                 else
                    bitwriter.putBit(false);
     }
    }
    intreader.close();
    bitwriter.close();

} </source> Decode <source lang="c"> void eliasGammaDecode(char* source, char* dest) {

    BitReader bitreader(source);
    BitWriter bitwriter(dest);
    int numberBits = 0;
    while(bitreader.hasLeft())
    {
       while(!bitreader.getBit() || bitreader.hasLeft())numberBits++; //keep on reading until we fetch a one...
       int current = 0;
       for (int a=0; a < numberBits; a++) //Read numberBits bits
       {
           if (bitreader.getBit())
              current += 1 << a;
       }
       //write it as a 32 bit number
       
       current= current | 1 ;              //last bit isn't encoded!
       for (int a=0; a < 32; a++) //Read numberBits bits
       {
           if (current & (1 << a))
              bitwriter.putBit(true);
           else
              bitwriter.putBit(false);
       }
    }

} </source>

See also

External links

Elias gamma coding

View More Summaries on Elias gamma coding
 
Ask any question on Elias gamma coding and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Elias gamma coding from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy