Let me start with the problem:
def word(byte1 : Byte, byte2 : Byte, byte3 : Byte, byte4: Byte) : Int = {
((byte4 << 0)) | ((byte3 << 8)) | ((byte2 << 16)) | ((byte1 << 24))
}
The goal here is pretty simple. Given 4 bytes, pack them in to an Int.
The code above does not work because it appears the shift operator tries to preserve the sign. For example, this:
word(0xFA.toByte, 0xFB.toByte, 0xFC.toByte, 0xFD.toByte).formatted("%02X")
Produces FFFFFFFD when I would have expected FAFBFCFD.
Making the problem smaller:
0xFE.toByte << 8
Produces -2 in two's complement, not 0xFE00.
How can I do a shift without the sign issues?
AND the bytes with 0xFF to undo the effects of sign extension before the shift:
((byte4 & 0xFF) << 0) | ((byte3 & 0xFF) << 8) | ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With