more useful options  
PerlMonks 
comment on 
( [id://3333]=superdoc: print w/replies, xml )  Need Help?? 
You say you don't know if your numbers will fit into 16 bit, and using 32 bit per number simply doubles storage space.
May I point you to the fact that there is an option in pack/unpack to store numbers with built in compression? They're called "BERcompressed integers" (see perlfunc:pack — use the "w" template), and they use a few bytes as possible. They're slightly less efficient in storage on the maximum number, as only 7 bits per byte are significant. But on the plus side: they don't waste any bytes they don't need, so for not so extreme values, the waste is no more than with the fixed length representation, and often less. Experimentally I've determined the following ranges for the byte count:
The range for 5 bytes runs at least up to 4294967295, i.e. 2**321, but above that, something goes wrong, at least on Indigoperl 5.6.1/Win32. Suddenly, I get a value for 2**32 that doesn't make any sense: it takes 10 bytes, for a start, while 2**321 only takes 5. In theory, there's no upper limit for which you could use this kind of representation, so I have no idea on what's going on. That problem aside, here's an extra way you can save on space: sort the numbers in ascending order, and store the difference between successing numbers — the first value being the smallest number itself, the difference from zero. The more numbers you have, the closer they'll be together, and that way, you might shave of a few bytes. In reply to Re: Compressing a set of integers
by bart

