Why is ~ -1 equal to 0 and ~ 1 equal to -2?

In accordance with subsection 11.4.8 of the ECMAScript 5.1 standard:

The product of UnaryExpression: ~ UnaryExpression is rated as follows:

  • Let expr be the result of evaluating UnaryExpression.
  • Let oldValue be ToInt32(GetValue(expr)) .
  • Returns the result of applying the bitwise complement to oldValue . The result is a signed 32-bit integer.

The ~ operator will call the internal ToInt32 method. In my understanding, ToInt32(1) and ToInt32(-1) will return the same value 1, but why is ~-1 equal to 0 and ~1 equal to -2?

Now my question is: why is ToInt32(-1) equal to -1? subsection 9.5 of the ECMAScript 5.1 standard:

The abstract operation ToInt32 converts its argument to one of two values 32 in the interval -2 31 through 2 31 -1 inclusive. This abstract operation functions as follows:

  • Let the number be the result of calling ToNumber on the input argument.
  • If the number is NaN, +0, -0, + ∞ or -∞, return +0.
  • Let posInt be the sign (number) * gender (abs (number)).
  • Let int32bit be posInt modulo 2 32 ; that is, the final integer k of the Number number of type with a positive sign and less than 2 32 in magnitude, so that the mathematical difference of posInt and k is mathematically an integer multiple of 2 32 .
  • If int32bit is greater than or equal to 2 31, return int32bit - 2 32 otherwise return int32bit.

when the argument is -1, according to 9.5, in step 1 the number will still be -1, skip step2 in step 3 posInt will be -1 in step 4 int32bit will be 1 in step 5 it will return 1

which step is wrong?

+4
source share
3 answers

-1 in 32 bit integer

 1111 1111 1111 1111 1111 1111 1111 1111 

So ~-1 will be

 0000 0000 0000 0000 0000 0000 0000 0000 

Which is zero.

1 in 32 bit integer

 0000 0000 0000 0000 0000 0000 0000 0001 

So ~1 will be

 1111 1111 1111 1111 1111 1111 1111 1110 

What is -2 .

You must read two additions to understand the mapping of a negative integer in a binary base.

+15
source

Where did you get the idea that ToInt32(-1) evaluates to 1? It evaluates to -1, which in a 32-bit binary representation with two extra bits is all the bits set to 1. When you use the ~ operator, each bit then becomes 0, which is 0 in 32-bit, two additional binary files .

Representation 1 is all bits 0, with the exception of bit 0. When the bits are inverted, the result is all bits 1, with the exception of bit 0. This occurs as two additional representations of -2. (To see this, simply subtract 1 from the two additional representations -1.)

+1
source
  • ToInt32(1) will return 1
  • ToInt32(-1) will return -1
  • -1 is represented as a signed 32-bit integer, having all 32 bits. A bitwise complement to this is that all bits are clear, so ~-1 gives 0
  • 1 is represented as a signed 32-bit integer, all even bits except the bottom bit. The bitwise complement to it contains all bits except the lower bit, which is a signed 32-bit integer representation of the value -2 . So ~1 gives -2
0
source

Source: https://habr.com/ru/post/1497285/


All Articles