Elias Delta Coding

來源:互聯網
上載者:User

EliasDelta Coding

適用範圍:

EliasDeltaCoding和EliasGamma Coding一樣,也是一種對正整數進行編碼的統一編碼,由PeterElias發明。適用於預先無法獲知最大編碼整數的情況,而且小整數出現頻率高,大整數出現頻率低。

 

編碼原理

對任何正整數NUM,對INT(Log2(NUM))+1進行Gamma編碼,尾碼上NUM二進位串除去最高位的子串。如5的編碼為011,01。

 

編碼樣本:

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

編碼、解碼演算法:

/****************************************************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 <<h;}num |= 1 << numPow;        pDecodedData[j++]=num;    }    nDecodedDataLen=j;return 1;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.