Unary code
From Wikipedia, the free encyclopedia
It is a method of representing numbers with a single alphabet, and hence called Unary code. If two alphabets are used we call it binary code, and so on.
Contents |
[edit] Encoding
This scheme can only encode numbers that are non-negative . The number is encoded by writing down N-0's (or 1's) followed by a stop character 1 (or 0).
UnaryEncode(N) = 000 ... N-times 0 . 1 eg: UnaryEncode(5) = 000001
[edit] Decoding
In decoding Unary codes it doesn't matter which of the two alternative encoding schemes (0 or 1 based) schemes. The algorithm reads the number of like (same) symbols till it encounters a stop symbols (different from the symbol read sofar) and declare the coded output.
UnaryDecode([0000 .. N-times 0 . 1]) = N eg: UnaryDecode([000001]) = 5
One can think of the decoder as a transition diagram with 2 states & actions associated with them.
Algorithm could be written is pseudocode (with 1 as terminating alphabet).
- Read First Symbol.
- If 1 declare output as N=0. Stop.
- If 0 ,{ N=N+1. Read Next, Goto 3}, Else Break.
- Declare output N. Stop
[edit] Applications
Unary codes are used in split-index schemes like Golomb Rice code. They are considered optimal in achieving the least entropy for sequences with exponential probability distribution function. Unary code is Prefix-free code and can be uniquely decoded.
[edit] References
Khalid Sayood, Data Compression, 3rd ed, Morgan Kaufmann. Professor K.R Rao, EE5359 Principles of Digital Video Coding. http://www.scheme.dk/blog/2006/04/compression-integer-encodings.html