We can read about Right shift operator >>
on MDN
… the sign of the resulting number is the same as the sign of the first operand.
However, 3332508426 >> 24 === -58
in my Chrome 127.0.6533.120.
How that could be explained?
By the way, 3332508426
is decimal representation of IP 198.162.11.10
.
4
Answers
Javascript bit manipulation works with 32-bit signed integers.
1<<31
for example gives-2147483648
Numeric values however use floating point
double
precision (64 bit IEEE754), so there is no problem in writing for examplex = 2147483648
and if you display
x
you get2147483648
(integers are represented without loss of accuracy up to ∓2⁵³ = ∓9007199254740992 … much more than 32 bit).For example however just shifting
x
by ZERO places you get -2147483648x << 0
returns-2147483648
because the computation is done using 32-bit signed integers.
3332508426 is HEX C6A20B0A.
If that is interpreted as 32 bit integer, the MSB is 1 so it is negative.
Right shifting keeps the sign.
MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift#description
As
3332508426
is already a 32-bit integer, it uses that value. However, 32-bit numbers are signed and the 32-bit binary representation of your number’s left-most bit is a 1, so is a negative value.The solution is to convert your number to
BigInt
first, then convert back, ie:Others have explained why it’s negative, but if your just after extracting the bytes from a Uint32,..
Another option, instead of bit shifting, if you want to extract the bytes from a Uint32, make a UInt32Array with the single value, and then cast that into a Uint8Array to extract the bytes.
eg.