Thanks to everyone in advance -
alert((~1).toString(2));
outputs: -10
but in PHP/Java it outputs 11111111111111111111111111111110
Am I missing something, why does Javascript add a "-" to the output?
Thx, Sam
Thanks to everyone in advance -
alert((~1).toString(2));
outputs: -10
but in PHP/Java it outputs 11111111111111111111111111111110
Am I missing something, why does Javascript add a "-" to the output?
Thx, Sam
I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got.
The way you calculate the negative of 10 (in base 2) using two's complement is that you first invert all of the bits, giving you:
11111111111111111111111111111101
then you add 1, giving you:
11111111111111111111111111111110
I guess the same is happening in Javascript.