0

I'm writing a program in Java that needs to make use of the four cardinal directions (North, East, South, and West). The problem I keep running into is how to represent each direction such that I use as little memory as possible. At first one would think that you can use a String to represent each direction as "North", "East", "South", and "West", but String comparisons are wasteful and not really necessary when all I have are four values I want to compare between. Not to even mention, the strings alone use a good chunk of memory beyond the scope of this problem.

I came up across the idea of using a boolean[] of length 2. There are only four possible combinations (00, 01, 10, and 11) which I could map to each direction. The problem I came across is that the boolean array will not use the two bits of space I expected it to in the memory. According to this answer: SO post, the array could take up to 4 additional Bytes of memory. Not really what I'm looking for...

After doing some more searching many posts and sites suggested the BitSet() object, but again, according to the post above, an object in java can take between 8 to 16 bytes of memory. Would anyone know how to best approach this problem in terms of keeping memory usage low? Preferably I would only like to use those two bits for representing the cardinal directions.

Is there really no way to represent (in Java) something like these four cardinal directions with exactly two bits? If not, what would be the most memory efficient representation of the cardinal directions for such a program?

Community
  • 1
  • 1
Greg
  • 435
  • 7
  • 18
  • 5
    Why are you so concerned about "memory efficiency" instead of readability? If things are so constrained that you're worried about bit packing, Java is the wrong platform. – chrylis -cautiouslyoptimistic- Mar 02 '17 at 22:30
  • "Would anyone know how to best approach this problem in terms of keeping memory usage low? " Store lots of them in the same `BitSet`. For a `BitSet` of length `2N`, you can store `N` directions; the storage overhead of the `BitSet` doesn't increase as the `BitSet` grows (aside from the actual `2N` bits of data you're storing, of course). – Andy Turner Mar 02 '17 at 22:35
  • 1
    As @chrylis suggests, you have your priorities wrong. This is about the _last_ thing you should be worrying about unless you intend to store _billions_ of such objects in memory at once. At which point the need to have them in memory at once should probably be re-examined – Jim Garrison Mar 02 '17 at 22:35
  • I mean to say, if I had no other option but to use Java for this problem, what would be the best approach? – Greg Mar 02 '17 at 22:39
  • 3
    @Greg To use an `enum Direction` and not worry about it. – chrylis -cautiouslyoptimistic- Mar 02 '17 at 22:42
  • @JimGarrison Maybe in a near future we will have billions of cardinal directions... –  Mar 02 '17 at 22:58

1 Answers1

0

you could use a byte[], each byte[] element taking 8 bits. So you can store 4 direction datasets in each byte. You can

1) set 0,1,2 or 3 as value for each direction (can be final static byte)
2) depending on the insert position in the byte, use the bitshift operations to create 
   a "mask" for your operation
3) use binary | operator to assign your value to the byte.

instead of bitshift, *4 or /4 (multiplication or division) will also shift by 2 bit digits...

Christoph Bimminger
  • 1,006
  • 7
  • 25