Im trying to compress a sequence of non-negative numbers, where:
- The value range of each number is from 0 to 2^N-1
Each number appears once only (it means there is total 2^N numbers )
Example for N = 4:
[14, 1, 8, 2, 12, 6, 0, 10, 4, 13, 5, 7, 15, 9, 3, 11]
So normally each number will cost 4 bits and for 16 numbers we will have to use 16x4 = 64 bits to store them.
Currently I have just thought of compressing them as below:
- For the first 8 numbers --> use 4 bits to store each of them.
- For the next 4 numbers ---> only 3 bits/each
- For the next 2 numbers ---> only 2 bits/each
- For the next 1 numbers ---> only 1 bit for it.
- For the last one, actually its not necesssary to be stored (Obviously we should know what the last number is if we know all other 15 numbers )
So the compressed data size will be:
Z = 8 * 4 + 4 * 3 + 2 * 2 + 1 * 1 + 1 * 0 = 49 bits
The compression ratio is about 76%, which is pretty good (I think).
But for larger values of N, the ratio seems to be decreased (For N = 2048, the ratio is only 91% )
So I would like to hear your suggestions for better compressing.
Thank you.