javascript - JS: Why is ~10 (binary, ~1010) = -11 (binary, -1011)? -


In Javascript, if I do not do bit bit bit bit I hope that it will not calculate bitwise on the binary integer:

 

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

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -