Encoding numbers from 0 to 255 using Huffman coding.

487 Views Asked by At

How can I encode numbers from 0 to 255 using Huffman coding (or any other code), so that each number (especially the largest numbers such as 255) wouldn't take 8 bits of binary space?

In other words, I want to write a number higher than 100, and lower than 256, in as many bits lower as possible than normal 8 bits.

I appreciate any help.

1

There are 1 best solutions below

0
On

You can't do it for all numbers because there are only $2^7=128$ seven bit combinations and you want to encode $256$ numbers. You can choose an encoding so some numbers use less than eight bits, but then some will use more than eight. Even just worrying about numbers $[100,255]$ there are more than $128$ of them so some will require at least eight bits.

Huffman coding works if some numbers are much more common than others. You assign short codes to the common numbers and long codes to the rare ones. You can reduce the average number of bits per number, but again some will have codes longer than eight bits.