How to efficiently store values, and benefit?

Essentially, I have an array of 3 sets of values, 0-3. I can store the 4 values in 2 bits, yeah? And each 3 values are related to each other - they represent rotation on X, Y, or Z. My question is, how would I go about storing these values in a single container in an array - preferably not a class as those add overhead?

Also, is there a gain in performance to storing and getting the values from these bits? Right now, I’m storing the values in a 4-dimensional byte array, as I have a 3D array of 3 values for each X, Y, and Z in space. I might just be jumping the gun, but this seems like bad practice to me. I’ve considered writing a utility class that uses a BitSet, but I’ve heard that those aren’t all they’re cracked up to be, simple though it might be.

For the former, original question, what I have boiled it down to is that I need to use bitshifting (> and <) as well as masking AND and OR in order to shift.


public void setBit(byte b, int pos, boolean truth) {
    if (truth) {
        b |= (1 << pos);
    }
    else {
        b &= ~(1 << pos);
    }
}

It’s kinda messy and that’s only how to SET the bits - I’m still unsure of how to read specific bits.

For the second method with the BitSet, it would be much easier - every six bits = the three 2-bit values, offset by 6 * whatever. But I don’t know if it’s a viable method, as the array size that I calculate I need for the values per “chunk” of data is like, ~3KB in bits alone. It would still be miles easier though… I think?

Sorry if anything is kinda oddly-worded!