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


当前回答

我知道这个问题的答案很久以前就贴出来了,但我想分享我的答案。

要找到一个数的一补,首先要找到它的二进制等价物。这里,十进制数字2用二进制形式表示为0000 0010。现在通过将其二进制表示的所有数字逆(将所有1都翻转为0,将所有0都翻转为1)来求其1的补数,这将得到:

0000 0010 → 1111 1101

这是十进制数2的1补。由于二进制数的第一个位,即符号位为1,这意味着它存储的数字的符号为负。(这里所指的数字不是2,而是2的1的补数)。

现在,由于数字存储为2的补数(取1的补数加1),所以要将这个二进制数1111 1101显示为十进制,首先我们需要找到它的2的补数,即:

1111 1101 → 0000 0010 + 1 → 0000 0011

这是2的补。二进制数0000 0011的十进制表示是3。并且,因为符号位是1,所以结果是-3。

提示:如果你仔细阅读这个过程,你会发现1的补码操作符的结果实际上是,数字(操作数-,这个操作符被应用)加1,带一个负号。你也可以用其他数字试试。

其他回答

Tl;dr ~翻转比特。结果符号就改变了。~2是负数(0b..101)。要输出一个负数红宝石打印-,则2的~2的补:-(~~2 + 1)== -(2 + 1)== 3。正数按原样输出。

有一个内部值,和它的字符串表示。对于正整数,它们基本重合:

irb(main):001:0> '%i' % 2
=> "2"
irb(main):002:0> 2
=> 2

后者相当于:

irb(main):003:0> 2.to_s
"2"

~翻转内部值的位。2 = 0b010。~2是0b..101。两个点(..)代表无限个1。由于结果的最高有效位(MSB)为1,因此结果为负数((~2)。= = true)。要输出一个负数的红宝石印-,则是二的内部补值。2的补位是通过翻转位,然后加1来计算的。0b的2的补。101等于3。是这样的:

irb(main):005:0> '%b' % 2
=> "10"
irb(main):006:0> '%b' % ~2
=> "..101"
irb(main):007:0> ~2
=> -3

总的来说,它翻转了位,从而改变了符号。为了输出一个负数,它输出-,然后~~2 + 1(~~2 == 2)。

ruby像这样输出负数的原因是,它将存储的值视为绝对值的2的补。换句话说,存储的是0b..101。它是一个负数,因此它是x的2的补,为了找到x,它是2的补0b..101。它是2的x的补,也就是x(例如~(~2 + 1)+ 1 == 2)。

如果你将~应用于一个负数,它只是翻转位(尽管如此,这改变了符号):

irb(main):008:0> '%b' % -3
=> "..101"
irb(main):009:0> '%b' % ~-3
=> "10"
irb(main):010:0> ~-3
=> 2

更令人困惑的是~0xffffff00 != 0xff(或MSB等于1的任何其他值)。让我们稍微简化一下:~0xf0 != 0x0f。这是因为它将0xf0视为正数。这是有道理的。因此,~0xf0 == 0x..f0f。结果是一个负数。0x的2的补。F0f是0xf1。所以:

irb(main):011:0> '%x' % ~0xf0
=> "..f0f"
irb(main):012:0> (~0xf0).to_s(16)
=> "-f1"

如果你不打算对结果应用位操作符,你可以考虑~作为-x - 1操作符:

irb(main):018:0> -2 - 1
=> -3
irb(main):019:0> --3 - 1
=> 2

但可以说,这并没有多大用处。

举个例子,假设你有一个8位的网络掩码(为了简单起见),你想计算0的个数。您可以通过翻转位并调用bit_length (0x0f. bit_length)来计算它们。bit_length == 4). But ~0xf0 == 0x..F0f,所以我们要去掉不需要的部分

irb(main):014:0> '%x' % (~0xf0 & 0xff)
=> "f"
irb(main):015:0> (~0xf0 & 0xff).bit_length
=> 4

或者你可以使用XOR运算符(^):

irb(main):016:0> i = 0xf0
irb(main):017:0> '%x' % i ^ ((1 << i.bit_length) - 1)
=> "f"

很简单:

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

首先,我们必须把给定的数字分成它的二进制数,然后把它颠倒过来,把最后一个二进制数相加。执行完后,我们必须给我们正在寻找补数的前一位数字赋相反的符号 ~ 2 = 3 解释: 2的二进制形式是00000010变成11111101,这是1的补码,然后补码为00000010+1=00000011,这是3的二进制形式,带-符号,即-3

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

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

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

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

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 

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