为什么~2等于-3?~操作符是如何工作的?


当前回答

很简单:

Before starting please remember that 
 1  Positive numbers are represented directly into the memory.
 2. Whereas, negative numbers are stored in the form of 2's compliment.
 3. If MSB(Most Significant bit) is 1 then the number is negative otherwise number is 
    positive.

你会发现~2:

Step:1 Represent 2 in a binary format 
       We will get, 0000 0010
Step:2 Now we have to find ~2(means 1's compliment of 2)
                  1's compliment       
       0000 0010 =================> 1111 1101 

       So, ~2 === 1111 1101, Here MSB(Most significant Bit) is 1(means negative value). So, 
       In memory it will be represented as 2's compliment(To find 2's compliment first we 
       have to find 1's compliment and then add 1 to it.)
Step3:  Finding 2's compliment of ~2 i.e 1111 1101

                   1's compliment                   Adding 1 to it
        1111 1101 =====================> 0000 0010 =================> 0000 0010
                                                                      +       1
                                                                      ---------
                                                                      0000 0011 
        So, 2's compliment of 1111 1101, is 0000 0011 

Step4:  Converting back to decimal format.
                   binary format
        0000 0011 ==============> 3
        
       In step2: we have seen that the number is negative number so the final answer would  
       be -3
                                    
                                So, ~2 === -3

其他回答

这个操作是补语,不是否定语。

考虑~0 = -1,然后从这里开始。

否定的算法是,“补,加”。

你知道吗?还有一种“一的补”,它的逆数是对称的,它有一个0和一个-0。

我认为对于大多数人来说,困惑的部分来自于十进制数和有符号二进制数的区别,所以让我们先澄清一下:

对于人类十进制世界: 01表示1, -01表示-1, 对于计算机的二进制世界: 101是无符号的,表示5。 101的意思是(-4 + 1),如果是有符号的数字在x位置。 | x

所以2的翻转位= ~2 = ~(010)= 101 = -4 + 1 = -3 这种混淆是由于混淆了有符号的结果(101=-3)和没有符号的结果(101=5)


位补操作符(~)是一个一元操作符。

它的工作原理如下

首先,它将给定的十进制数转换为相应的二进制数 价值。这是在2的情况下,它首先将2转换为0000 0010(到8位二进制数)。

然后它将数字中的所有1都转换为0,所有0都转换为1,然后数字将变成11111101。

这是-3的2的补表示。

为了找到无符号的值使用补,即。要简单地将1111 1101转换为十进制(=4294967293),只需在打印时使用%u。

Javascript波浪号(~)将给定值强制转换为1的补位——所有位都是反向的。 这就是波浪的作用。这不是固执己见。它既不加也不减任何量。

0 -> 1
1 -> 0
...in every bit position [0...integer nbr of bits - 1]

On standard desktop processors using high-level languages like JavaScript, BASE10 signed arithmetic is the most common, but keep in mind, it's not the only kind. Bits at the CPU level are subject to interpretation based on a number of factors. At the 'code' level, in this case JavaScript, they are interpreted as a 32-bit signed integer by definition (let's leave floats out of this). Think of it as quantum, those 32-bits represent many possible values all at once. It depends entirely on the converting lens you view them through.

JavaScript Tilde operation (1's complement)

BASE2 lens
~0001 -> 1110  - end result of ~ bitwise operation

BASE10 Signed lens (typical JS implementation)
~1  -> -2 

BASE10 Unsigned lens 
~1  -> 14 

以上所有观点同时都是正确的。

正如其他人所提到的~只是翻转位(将1变为0,将0变为1),由于使用了2的补码,您就得到了您所看到的结果。

需要补充的一点是为什么使用2的补数,这是为了对负数的运算和对正数的运算是一样的。把-3看成是要加3才能得到0的数字,你会看到这个数字是1101,记住二进制加法就像小学(十进制)加法,只是你得到2时进1,而不是10。

 1101 +
 0011 // 3
    =
10000
    =
 0000 // lose carry bit because integers have a constant number of bits.

因此1101是-3,翻转位就得到0010,也就是2。