简体   繁体   English

为什么8位字节中的〜1等于-2?

[英]Why is ~1 in an 8-bit byte equal to -2?

I would expect that a, due to a NOT 00000001 would turn into 11111110, otherwise known as 127, or -126 if counting the far left bit as the sign, if sign&magnitude was used. 我希望由于NOT 00000001而将a变成11111110,否则称为127,或者如果使用sign&magnitude,则如果将最左边的位算作符号,则将变为-126。

Even in the instance of 2s compliment, I would expect the answer to result in -127 即使在2s恭维的情况下,我希望答案也会导致-127

Why is it that the result is -2? 为什么结果是-2?

In two's complement: 补码:

-x = ~x + 1

By subtracting one from both sides we can see that: 通过从两侧减去一个,我们可以看到:

~x = -x - 1

And so in your example, if we set x = 1 we get: 因此,在您的示例中,如果我们将x = 1设置x = 1

~1 = -1 - 1 = -2

Consider how the numbers wrap around. 考虑数字如何环绕。

If we start with 00000010 (2) and take away one then it is: 如果我们以00000010 (2)开头并带走一个,则为:

  00000010
- 00000001
 ---------
  00000001

Which is 1. We "borrow 1" from the column to the left just as we do with decimal subtraction, except that because it's binary 10 - 1 is 1 rather than 9 . 这是1.“借1”从列就像我们做十进制减法左侧,除了因为它是二进制的10 - 11 ,而不是9

Take 1 away again and we of course get zero: 再取一,我们当然得到零:

  00000001
- 00000001
 ---------
  00000000

Now, take 1 away from that, and we're borrowing 1 from the column to the left every time, and that borrowing wraps us around, so 0 - 1 = -1 is: 现在,从中减去1,我们每次都从列的左侧借入1,并且借用使我们环绕,所以0-1 0 - 1 = -1为:

  00000000
- 00000001
-----------
  11111111

So -1 is all-ones. 所以-1是全1。

This is even easier to see in the other direction, in that 11111111 plus one must be 00000000 as it keeps carrying one until it is lost to the left, so if x is 11111111 then it must be the case that x + 1 == 0 , so it must be -1. 从另一个方向看,这甚至更容易理解,因为11111111加一个必须为00000000因为它将一直携带一个直到丢失到左边为止,因此,如果x为11111111那么必然是x + 1 == 0 ,因此必须为-1。

Take away another one and we have: 带走另一个,我们有:

  11111111
- 00000001
  --------
  11111110

So -2 is 1111110 , and of course ~1 means flipping every bit of 00000001 , which is also 11111110 . 所以-21111110 ,当然~1意味着翻转00000001每一位,这也是11111110 So ~1 must be -2 . 因此~1必须为-2

Another factor to note here though is that arithmetic and complements in C# always converts up to int for anything smaller. 不过,这里要注意的另一个因素是C#中的算术和补码总是将较小的任何内容转换为int For a byte the value 11111110 is 254 , but because ~ casts up to int first you get -2 rather than 254. 对于一个byte ,值11111110254 ,但是因为~将其强制转换为int您得到-2而不是254。

byte b = 1;
var i = ~b; // i is an int, and is -2
b = unchecked((byte)~b); // Forced back into a byte, now is 254

To convert a negative 2-compliment number to its decimal representation we have to: 要将负的2补码数转换为其十进制表示形式,我们必须:

  • start scanning the bitstring from right to left, until the first '1' is encountered 开始从右到左扫描位串,直到遇到第一个“ 1”
  • start inverting every bit to the left of that first '1' 开始将第一个“ 1”左侧的每一位都反转

Thus, in 11111110 we see the sign bit is 1 (negative number), and above method yields the number 000000010 , which is a decimal 2. In total, we thus get -2 . 因此,在11111110我们看到符号位是1 (负数),以上方法产生的数字是000000010 ,它是一个十进制2。总的来说,我们得到-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM