Eliasdelta Coding
Applicability:
Like eliasdeltacoding and eliasgamma coding, eliasdeltacoding is a unified encoding of positive integers, invented by peterelias. It is applicable to situations where the maximum encoded integer cannot be known in advance, and small integers are frequently used and large integers are frequently used.
Encoding Principle:
For any positive integer num, the INT (log2 (Num) + 1 is gamma encoded, And the num binary string on the suffix removes the highest substring. For example, 5 is encoded as 011,01.
Encoding example:
Num |
Eliasdelta code |
Implied Probability |
1 = 20 +0 |
1 |
1/2 |
2 = 21 +0 |
0100 |
1/16 |
3 = 21 +1 |
0101 |
1/16 |
4 = 22 +0 |
01100 |
1/32 |
5 = 22 +1 |
01101 |
1/32 |
6 = 22 +2 |
01110 |
1/32 |
7 = 22 +3 |
01111 |
1/32 |
8 = 23 +0 |
00100000 |
1/256 |
9 = 23 +1 |
00100001 |
1/256 |
Encoding and decoding algorithms:
/****************************************************Encode_EliasDelta:Encoding algorithm of EliasDelta Coding.*****************************************************/int Encode_EliasDelta(int *pSourceData,char *pEncodedData,int nSourceDataLen,int &nEncodedDataLen){int k=-1; for(int i=0;i<nSourceDataLen;i++) { int num = pSourceData[i]; int numPow= int(log10(num)/log10(2));int num1 = numPow+1;int num1Pow= int(log10(num1)/log10(2));for (int j=0; j <num1Pow ; j++) pEncodedData[++k]=0;pEncodedData[++k]=1; for (j=num1Pow-1; j >= 0; j--) { if (num1 & 1 << j) pEncodedData[++k]=1; else pEncodedData[++k]=0;} for (j=numPow-1; j >= 0; j--) { if (num & 1 << j) pEncodedData[++k]=1; else pEncodedData[++k]=0; }nEncodedDataLen=k+1;}return 1;}/****************************************************Decode_EliasDelta:Decoding algorithm of EliasDelta Coding.*****************************************************/int Decode_EliasDelta(int *pDecodedData,char *pEncodedData,int &nDecodedDataLen,int nEncodedDataLen){int i=0,j=0;while (1) {//Decode num1 int num1Pow = 0; while (!pEncodedData[i++]) num1Pow++; if(num1Pow >=48) break; int num1 = 0; for (int h=num1Pow-1; h >= 0; h--)if (pEncodedData[i++]) num1 |= 1 << h; num1 |= 1 << num1Pow; //Decode numint numPow = 0;numPow = num1-1;int num =0;for( h=numPow-1;h>=0;h--){if(pEncodedData[i++])num |= 1 <