An integer is usually 4 bytes. But if you store a small number, such as 99, the other three bytes contain 8x 0-digit bits. The specification asks you to use one integer storage (4 bytes) to store 4 different smaller numbers in its bytes.
The easiest way is probably to use the toInt function in an array of 4 bytes, for example. (checking byte length [] is not checked and this function is not checked - this is just an illustration)
public static final int toInt(byte[] b) { int l = 0; l |= b[0] & 0xFF; l <<= 8; l |= b[1] & 0xFF; l <<= 8; l |= b[2] & 0xFF; l <<= 8; l |= b[3] & 0xFF; return l; } byte[] bytes = new byte[] {99, 4, 9, 0}; int i = toInt(bytes, 0);
32-bit int
11110101 00000100 00001001 00000000 ^byte ^byte ^byte ^byte
Each block of 8 bits in int is enough to "encode" / "save" a smaller number. So int can be used to crush 4 smaller numbers.
source share