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
.
1
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 example
x = 2147483648
and if you display x
you get 2147483648
(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 -2147483648
x << 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.