javascript - JS: Why is ~10 (binary, ~1010) = -11 (binary, -1011)? -
In Javascript, if I do not do
Code> ~ 1010 = 0101
In other words, I was expecting the decimal integer 5. Instead, the Operation gives me-11 (if you see it in the console)
~ 10 = -11
If I use that -11 and ~ Seeing 10 as binary integer string:
parseInt (~ 10,10) .toString (2) "-1011" parseInt (-11,10) .string (2) "-1011"
Compatible but I do not understand. Can anyone explain to me why? I think it has to do something with the signal.
EDIT: After posting, it also helped me understand this phenomenon better.
Bitwise operators in Javascript consider the operator number as 32 bits. Then the result is:
00000000 00000000 00000000 00001010
When you reverse it, the result is:
11111111 11111111 11111111 11110101
When a 32-bit sign is interpreted as a number, then -11 (if you do not understand, read Wikipedia entry).
Comments
Post a Comment