| Elias Gamma Coding |
Article Index for Elias |
Shopping Coding |
Website Links For Elias |
Information AboutElias Gamma Coding |
| CATEGORIES ABOUT ELIAS GAMMA CODING | |
| numeration | |
| lossless compression algorithms | |
|
To code a Number : #Write it in Binary . #Subtract 1 from the number of bits written in step 1 and prepend that many zeros. An equivalent way to express the same process: #Separate the integer into the highest power of 2 it contains (2''N'') and the remaining ''N'' binary digits of the integer. #Encode ''N'' in Unary ; that is, as ''N'' zeroes followed by a one. #Append the remaining ''N'' binary digits to this representation of ''N''. The code begins: 1 = 20 + ''0'' = 1 2 = 21 + ''0'' = 01''0'' 3 = 21 + ''1'' = 01''1'' 4 = 22 + ''0'' = 001''00'' 5 = 22 + ''1'' = 001''01'' 6 = 22 + ''2'' = 001''10'' 7 = 22 + ''3'' = 001''11'' 8 = 23 + ''0'' = 0001''000'' 9 = 23 + ''1'' = 0001''001'' 10 = 23 + ''2'' = 0001''010'' 11 = 23 + ''3'' = 0001''011'' 12 = 23 + ''4'' = 0001''100'' 13 = 23 + ''5'' = 0001''101'' 14 = 23 + ''6'' = 0001''110'' 15 = 23 + ''7'' = 0001''111'' 16 = 24 + ''0'' = 00001''0000'' 17 = 24 + ''1'' = 00001''0001'' To decode an Elias gamma-coded integer: #Read and count 0s from the stream until you reach the first 1. Call this count of zeroes ''N''. #Considering the one that was reached to be the first digit of the integer, with a value of 2''N'', 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. 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
{ 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 0's to indicate how much bits that will follow } bitwriter.putBit(true);//mark the end of the 0's 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(); } Decode
{ BitReader bitreader(source); BitWriter bitwriter(dest); int numberBits = 0; while(bitreader.hasLeft()) {   |
Current |
current 1 //last bit isn't encoded! |
|
|
|