4 ms·
java virtually has only 2 integer types int and long, or 4 and 8 bytes one. So all operations are converted to int (or long) 1st, even when they involve byte/sh
by xxs 5y ago
java virtually has only 2 integer types int and long, or 4 and 8 bytes one. So all operations are converted to int (or long) 1st, even when they involve byte/short, etc.
Imagine you try to combine a 4bytes (from an array) into a single int. If you just bitwise OR and shift left, e.g.
something like
(b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
it won't work, you need ((b[1] & 0ff) << 16), etc. The byte values greater than 127 would have the sign bit set (and all other higher bits to make the 2complimentary form)
That part is somewhat alleviated by wrapping byte arrays into ByteBuffers that would the right thing, or even better using only ByteBuffers (preferably the direct version of them when reading from input/output)