compress 2 bytes into 1 byte

Hello! I had an idea earlier today, don’t worry about what it’s for; just a test I want to do for my game :slight_smile: I already got the “dont do this” from TheAgentD :stuck_out_tongue:

When reading texture data from 2 files, I only use the red channel, and I want to compress the red values of both textures into 1 byte (2 bytes --> 1 byte).

I tried:

byte b1 = (byte) 200;
byte b2 = (byte) 255;

int i = ((b1 << 8) | (b2 & 0xFF));
System.out.println(i);

int val1 = i >> 8 & 0xFF;
int val2 = i & 0xFF;
System.out.println("b1: " + val1);
System.out.println("b2: " + val2);

Which… well… works, but not exactly, as the variable i is an integer, not a byte.

I know there will be some precision loss by doing this, but it’s for texture data that doesn’t need to be 100% accurate, just close.

How do I do this properly? and how would I extract the two bytes back out.

The only other thing I’ve thought of, is have one texture limited to 0, 1, or 2; and have the other limited to 0 to 55.