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


当前回答

下面是一种解释:

让我们以~2 = -3为例(为了简单起见,使用8位系统进行解释)

1)我们有2——> 00000010

2)我们可以得到~2—> 11111101 #通过简单地交换位。

[但常见的错误是,有些人试图将~2的二进制值直接转换为十进制(以10为基数)数字,在这种情况下,它是253。这不是我们寻找互补的方式。

3)现在我们找到一个二进制数,将其与二进制值2~相加得到0(00000000)作为结果。 在这种情况下,它是00000011(即3),因为如果我们将00000011加到我们已有的11111101,我们得到100000000,但由于我们使用的是8位系统,1在第9位,它被完全忽略,所以我们最终得到00000000。

4)从点(3)我们可以说~2+3 = 0,因此我们可以说~2 = -3

注意:-3的值是简单的11111101,可以用同样的方式解释。

其他回答

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

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

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

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

这里,二进制(8位)中的2是00000010,它的1的补码是11111101, 1的补数减去1得到1111110 -1 = 11111100, 这里的符号是-作为第8个字符(从R到L)是1 求1的补,no。即00000011 = 3 符号是负的所以这里是-3。

int = 4; System.out.println (~); 结果是:-5

Java中任意整数的“~”表示1对no的补。 例如,我取~4,这意味着用二进制表示0100。 首先, 整数长度为4字节,i。e4 *8(8位1字节)=32。 在系统内存中,4表示为 0000 0000 0000 0000 0000 0000 0000 0100 现在~操作符将对上面的二进制no执行1的补

i.e 1111 1111 1111 1111 1111 1111 1111 1011->1's complement the most significant bit represents sign of the no(either - or +) if it is 1 then sign is '-' if it is 0 then sign is '+' as per this our result is a negative number, in java the negative numbers are stored in 2's complement form, the acquired result we have to convert into 2's complement( first perform 1's complement and just add 1 to 1's complement). all the one will become zeros,except most significant bit 1(which is our sign representation of the number,that means for remaining 31 bits 1111 1111 1111 1111 1111 1111 1111 1011 (acquired result of ~ operator) 1000 0000 0000 0000 0000 0000 0000 0100 (1's complement)

1(2的补数)

1000000 0000 0000 0000 0000 0000 0000 0101 现在结果是-5 查看视频<[java中的位运算符]https://youtu.be/w4pJ4cGWe9Y的链接

很简单:

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