Unary Code

来源:互联网 发布:2017大数据的发展现状 编辑:程序博客网 时间:2024/04/27 05:23
  • Unary coding, sometimes called thermometer code, is an entropy encoding that represents a natural number n, with n ones followed by a zero (if natural number is understood as non-negative integer) or with n − 1 ones followed by a zero (if natural number is understood as strictly positive integer).
  • For example 5 is represented as 111110 or 11110. Some representations use n or n − 1 zeros followed by a one. The ones and zeros are interchangeable without loss of generality.
  • Unary coding is both a prefix-free code and a self-synchronizing code.
n (non-negative)n (strictly positive)Unary codeAlternative01011210012311000134111000014511110000015611111000000167111111000000017811111110000000018911111111000000000191011111111100000000001
  • A generalized version of unary coding is able to represent numbers much more efficiently than standard unary coding.
  • Here’s an example of generalized unary coding for integers from 1 through 15 that requires only 7 bits (where three bits are arbitrarily chosen in place of a single one in standard unary to show the number). Note that the representation is cyclic where one uses markers to represent higher integers in higher cycles.
nUnary codeGeneralized unary000000000110000011121100001110311100011100411110011100051111101110000611111100010111711111110010111081111111101011100911111111100111001101111111111011100101111111111111001001111211111111111101001110131111111111111000111011411111111111111001110101511111111111111101110100