convert signed byte to unsigned byte, javascript:
-5 & 0xff // = 251 , signed to unsigned byte
251 <<24 >>24 // = -5 , unsinged byte to signed
the first makes all first bits 0 except the 1st byte
the second found on
https://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html
basically, in a number, there are 4 bytes.
in a positive number, the 3 first bytes are 0 and all 0 bits are zero.
in a negative number, the 3 first bytes are 1 and all 0 bits are one and all 1 bits are zero. In bytes the most significant bit of 4th byte is used for sign;
shifting bits to left makes the 1st bit 1 of 1st byte to be as the 1st bit of 4th byte, then shifting again to the right, drags the most significant bit across. so if the sign bit is 1. it makes many 1 bits in front 3 bytes. so it is a side effect of shifting but it works.
for example: like you start from
?1111111 the first bit is a sign bit - ?,
but in a larger variable, so it is:
00000000_00000000_00000000_?1111111
shift to left
?1111111_........_........_........
shift to right
????????_????????_????????_?1111111
this is the effect,
it drags the edge bit across the shift