Some crash course about bit packing (i'm using hex numbers which makes it easy to understand):
int32 a = 0x01, b = 0x02, c = 0x55;
All numbers must be >=0 and <= 0xFF (255) - for that range we need 8 bits because 2^8 = 256 possibilities.
int32 compressed = a | (b<<8) | (c<<16);
We get 0x00550201 by bit shifting and OR to pack 3 numbers into one. (upper most 8 bits still unused - you could pack a 4th number)
Now unpack:
int32 A = compressed & 0xFF; // mask out the other numbers
int32 B = (compressed>>8) & 0xFF; // bit shift and mask
int32 C = (compressed>>16); // no mask necessary if there is no 4th number
What to do if our original numbers are larger, but we want to use only 8 bits?
int32 a = 0x00120004;
We could ignore less important bits:
int32 qA = a>>16; // 0x00000012
then after packing and unpacking you lost 16 bits, resulting in 0x00120000 but sometimes that's good enough. Edit: E.g. if you bin characters to smaller tiles of space, less bits become enough for the position relative to the tile, which is a form of delta compression.
2 hours ago, Scouting Ninja said:
2 hours ago, Kylotan said:
One of 8 directions can be stored and transmitted in 3 bits.
What would the code for this look like?
You would need to tell how you store your original data. In the worst case it's a switch with 8 cases.