0

I'm interested in making a class to store data in a more pact manor and I know I can store numbers between 0 and 2^(x)-1 with ease, along with booleans by using bitwise operators, but I'm interested in more odd numbers like a max of 6 or something. I've been fiddling with it in my mind for about a week now and have been googling for about an hour now but I can't really find anything of help.

I'm looking for an algorithm or something to help me figure out how best to pack data that is between 0 and a random number that's between 1 and 32ish...

I'm just brain storming on this and would like to also know if compression like this should even be looked at. one of the reasons why I'm looking into this is for huge arrays and such. Sorry if this is a stupid question, my brain hasn't been very sharp recently.

Also, an example of max values: 1,5,8,3,12,19

A finalized class I was thinking of would be something like:

public class MyObject{
  private long packed;
  ...
  public int getA(){...}
  public void setA(){...}
  public int getB(){...}
  public void setB(){...}

Thanks for the help, ~vzybilly~

vzybilly
  • 323
  • 3
  • 14
  • I'm interested in more extreme data compression while still maintaining usability of the data. I have afew projects on the mind that grow exponentially and such, so I wanted to make a class that could build new classes that holds the desired data. I also remember an online chat I was in did something to store 0~2 and 0~5 while managing to put it into 4 bits... but I forgot the things they did to make that happen and my best is 5 bits, an additional 25% – vzybilly May 16 '14 at 01:19
  • @vzybilly If your data requirements grow exponentially bit packing isn't going to help much, since you only get a linear compression rate at best. – awksp May 16 '14 at 01:35
  • have a look at this link http://www.oracle.com/technetwork/articles/java/compress-1565076.html and consider using zip file compression - already done for ya – Scary Wombat May 16 '14 at 02:19
  • even though it won't help much, it can still be useful and can atleast put it off so it can hopefully do the things it needs to. I thought that ZIP was more for files and network, I'm not sure that it could work as a data type in an efficient manor like other ways... – vzybilly May 16 '14 at 02:49

1 Answers1

0

As a general question it's not very interesting to a programmer - maybe to a mathematician/information theorist. Specific examples are fun to play with but not important in today's world where memory is plentiful but all the other work you and computer do is more scarce.

Let's say you had to store a sequence of a lot numbers in the range 1-6. You can fit 12 of these in a 32-bit integer, as 6^12 < 2^32. But now you have extra arithmetic whenever you access data, basically seeing how many 6^n's there are in the entry modulus 6^n+1. This is because there are tradeoffs in speed and compactness for whichever representation you use. "Best" in practice means straightforward.

It's not to say there aren't times when saving space is free, good, and elegant, however it is an issue that should only be asked by one who already has the skills to solve it, in a situation that requires it.

clwhisk
  • 1,805
  • 1
  • 18
  • 17
  • I'm interested in learning the skills, even though I might not need them right now, it can be helpful. I also already know shifting and ORing them together to pack and such but was looking for a more compact way of doing it – vzybilly May 16 '14 at 01:23
  • Basically, you just store a group of 1-6 values as a bigger number in base 6. You're not doing shifting and ORing; you're doing multiplying and modding. – Louis Wasserman May 16 '14 at 01:39
  • How would I store/retrieve the data using that, I think that is more of what I'm interested in... I'll see if I can't fiddle and test out afew methods of doing it – vzybilly May 16 '14 at 02:45
  • I think I got it with `public int[] unpack(int pack){return new int[]{pack / max[0],pack % max[0]};} public int pack(int[] num){return (num[0] % (max[0]+1)) * max[0] + (num[1] % (max[1]+1));}` is that the best way? – vzybilly May 16 '14 at 18:24
  • I'm too slow to understand your notation, but think of it as using the decimal (binary, trinary, etc) system: if you have numbers 0-9 (2,7,1,8,2,8) to store, a compact way is to store the number 271828 which is 8*10^0 + 2*10^1 + 8*10^2... and so on. Never mind that the memory is in binary, you can store the number and decipher it in the base of your choosing. This is the "best" way spacewise because you can prove if there were any more possibilities, it would require more space to represent. – clwhisk May 17 '14 at 06:28
  • But there are methods where occasional mistakes are allowed. Didn't take "information theory" but it sounded fun. Google Shannon, Huffman codes... Also, you could adapt the method to other data, such as representing a telephone number where there are restrictions on some of the digits. You would use a different exponent in each "digit" – clwhisk May 17 '14 at 06:31