在一个补码系统中,为了显示负二进制数,我们只需对每个位进行补码。例如:+3= 0011,-3= 1100
在二进制补码系统中,我们使用进位位检测溢出,但是一个补码呢?
在一个补码系统中,为了显示负二进制数,我们只需对每个位进行补码。例如:+3= 0011,-3= 1100
在二进制补码系统中,我们使用进位位检测溢出,但是一个补码呢?
检测二进制补码和溢出的规则很简单,我也推导出 1' 补码:
加法与 2 的补码相同,不同之处在于它不是丢弃最后一个进位,而是添加到最右边位置的 n 位和(“end-around”进位)。看看下面的例子:
Pos + Neg numbers, Neg > Pos
+5 0101
-6 1001
-1 1110 (correct answer)
不同的符号,不会发生溢出。
Pos + Neg numbers, Pos > Neg
-5 1010
+6 0110
(1) 0000
1 (end-around carry)
+1 0001 (correct; no overflow)
结束进位发生,但没有任何溢出
-3 1100
-4 1011
(1) 0111
1 (end-around carry)
-7 1000 (correct; no overflow)
|sum| 的负数 <= 2^(n-1)
-5 1010
-6 1001
(1) 0011
1 (end-around carry)
0100 (wrong answer; overflow)
负数|sum| > 2^(n-1)。此外,两者具有相同的相似符号,结果具有不同的符号。
对于一个补码,您可以使用与二进制补码相同的溢出条件。
让我用 3 位数来说明。
一个人的补充:
011 -4 overflows into +3
100 -3 lowest possible number
101 -2
110 -1
111 -0
000 +0
001 +1
010 +2
011 +3 highest possible number
100 +4 overflows into -3
二进制补码:
011 -5 overflows into +3
100 -4 lowest possible number
101 -3
110 -2
111 -1
000 0
001 +1
010 +2
011 +3 highest possible number
100 +4 overflows into -4
在这两个系统中,都可以通过查看最高有效位来检测溢出。发生溢出: