LSB/MSB handling in Java
问题 If I have to handle values to be stored in bytes like 0x118, how do I split the LSB and MSB? I was trying the following way... I don't think that it's the right way: value = 0x118; Storing in bytes... result[5] = (byte) value; result[6] = (byte)(value << 8); ... What is the correct way? 回答1: This will do it: result[5] = (byte) (value & 0xFF); // Least significant "byte" result[6] = (byte) ((value & 0xFF00) >> 8); // Most significant "byte" I usually use bit masks - maybe they're not needed.