I'm looking to store 4 unsigned values, one boolean (or int), two integers with a max of (and including) 64 and one integer which can store at least 100,000. into a single hash.
Using information I found here
I can encode and decode between 2 - 4 integer's with a max of 255 like so
static int encode(int a, int b, int c, int d) {
return a & 0xff | (b << 8) | (c << 16) + (d << 24);
}
static int[] decode(int encoded) {
return new int[] {
encoded & 0xff,
(encoded >> 8 & 0xff),
(encoded >> 16 & 0xff),
(encoded >> 24 & 0xff)
};
}
And using information found here I can encode and decode two 32 bit integers.
long hash = (long) a << 32 | b & 0xFFFFFFFFL;
int aBack = (int) (hash >> 32);
int bBack = (int) hash;
I just don't understand bitwise operators well enough to figure out how to mix and match to store integers of different sizes.
How can I use bit masks to encode 4 integers of different sizes into one integer and back?