DataInputStream#readUnsignedShort return a int
which spend 32 bits, but the short
type spend 16 bits.
int << 16
will shift the low 16 bits to high 16 bits and fills 0
in the low 16 bits. for example:
int value = 0b11111111000000001000000011111111;
^---------------
int high = 0b10000000111111110000000000000000;
// high == value << 16
in this case, and the |
operator is joins high bits and low bits together. for example:
int high = 0b10000000111111110000000000000000;
int low = 0b00000000000000001000000000000001;
int value = 0b10000000111111111000000000000001;
// value == high | low;
on the other hand, your protocol saving an int
into two short
s which are a high short
and a low short
. and the code above is read the int
value from two short
s.
for more details you can see bitewise operators and shift operators.