简体   繁体   English

计算机如何添加负数?

[英]How do computers add negative numbers?

So in twos complement, -1 is represented as 11111111 as an 8-bit number. 因此,在二进制补码中,-1表示为11111111,为8位数。

so how does 怎么样

11111111
11111111+
_________
11111110

The same as adding positive numbers! 与添加正数相同!

1 + 1 = 10, right? 1 + 1 = 10,对吗? So the true addition is: 所以真正的补充是:

  11111111
  11111111+
 _________
 111111110

but we're using fixed-length 8-bit arithmetic, so the leftmost bit is lost, giving the answer 但我们使用的是固定长度的8位算术,所以最左边的位丢失了,给出了答案

  11111110

or -2. 或-2。

Another way to look at this is that 另一种看待这种情况的方法是
bit 0 has value 1 位0的值为1
bit 1 has value 2 位1的值为2
bit 2 has value 4 第2位的值为4
.. ..
bit 6 has value 64 第6位的值为64
bit 7 has value -128 (this is 'the sign bit') 第7位的值为-128(这是'符号位')

You'd add it just like you would any other value. 你可以像添加任何其他值一样添加它。 The addition of the two, least significant bits result in overflow, and the bit is carried over to be added to the second, least significant bit, and so on. 添加两个最低有效位会导致溢出,并且该位被转移以添加到第二个最低有效位,依此类推。 Once the two, most significant bits are added, the bit that is carried over (due to overflow) is simply discarded. 一旦添加了两个最高有效位,就会丢弃被携带的位(由于溢出)。

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

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