# compression problem

This topic is 2613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering if someone could help me with some compression.

I want to compress a variable amount of numbers up to 20 into one number.

So I could have like 1,3,7,15.

or 5, 16, 20

I want those numbers compressed into one but also want to extract them from compressed form.

I don't have any experience with compression so if anyone could help that would be awesome.

##### Share on other sites
If the numbers are ascending and don't repeat, I guess you could use some 20bit (or more, generally 32bit) integer type and just set a bit if the corresponding number is selected or not. If the numbers are ordered or can repeat, you'll probably have to use 5 bits to represent each number. For simplicity, going with a 32bit integer type, you could store 32 / 5 bits, or approximately 6 numbers per each integer, just shift the bits into place.

##### Share on other sites
The ideal method of compression depends upon several things:
What is the frequency distribution of the values? I.e. are all values from 1-20 (I assume zero is not included?) equally likely, or is it the case that the smaller the number is the more likely it is?
If the distribution is uneven then can you describe the distribution? For example is each number half as likely as the one before it (like the number of heads in a row when coin tossing).

Are duplicates possible?

Is this going to file, or across a network, or just for fitting a lot of data into RAM?

Do you want to be able to access the Nth compressed number I.e. random access, or is it okay to only be able to stream the numbers in and process them in order?

How important is size reduction vs time taken (say on a one to ten scale where one means "some reduction would be nice, but it really has to be fast" and ten means "must save every last bit as if my life depends on it".

1. 1
2. 2
Rutin
24
3. 3
JoeJ
19
4. 4
5. 5

• 17
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631729
• Total Posts
3001918
×