我只是好奇,为了在二进制中表示-1,是否有一个原因,使用了2的补码:翻转位并加1?

-1由11111111(2的补码)表示,而不是(对我来说更直观)10000001,10000001是二进制1,第一位为负标志。

免责声明:我的工作不依赖二进制算术!


当前回答

为什么用Two2的补语系统来表示负数,而不是用One的补语系统,一个令人满意的答案是 二的补语系统解决了一的补语系统中存在的表示负数的0的多重表示和对进位的需要。

欲了解更多信息,请访问https://en.wikipedia.org/wiki/Signed_number_representations

用于末端绕行访问 https://en.wikipedia.org/wiki/End-around_carry

其他回答

这是为了简化数字的和和和差。2的补数中一个负数和一个正数的和与正常方式的和是一样的。

2的补语允许负数和正数相加,而不需要任何特殊的逻辑。

如果你想用你的方法做1和-1相加 10000001 (1) + 00000001 (1) 你得到 10000010 (2)

相反,通过使用2的补数,我们可以相加

11111111 (1) + 00000001 (1) 你得到 00000000 (0)

减法也是如此。

同样,如果你试着用6减去4(两个正数),你可以用2补4,然后把两者相加6 + (-4)= 6 -4 = 2

这意味着正数和负数的减法和加法都可以由cpu中的同一个电路完成。

维基百科说明了一切:

二补系统的优点是不需要加减电路检查操作数的符号来决定是加还是减。这一特性使系统实现更简单,能够轻松地处理更高精度的算术。此外,零只有一种表示,避免了与负零相关的微妙之处,这种微妙之处存在于补体系统中。

换句话说,无论数字是否为负,加法都是一样的。

It's worthwhile to note that on some early adding machines, before the days of digital computers, subtraction would be performed by having the operator enter values using a different colored set of legends on each key (so each key would enter nine minus the number to be subtracted), and press a special button would would assume a carry into a calculation. Thus, on a six-digit machine, to subtract 1234 from a value, the operator would hit keys that would normally indicate "998,765" and hit a button to add that value plus one to the calculation in progress. Two's complement arithmetic is simply the binary equivalent of that earlier "ten's-complement" arithmetic.

该操作的通常实现是“翻转位并加1”,但有另一种定义它的方式可能使其原理更清楚。2的补数是通常的无符号表示形式,其中每一位控制2的下一次方,并使最有效项为负。

取8位值a7 a6 a5 a4 a3 a2 a1 a0

通常的无符号二进制解释是: 27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

两人的补充解释是: -27*a7 + 26*a6 + 25*a5 + 24*a4 + 23*a3 + 22*a2 + 21*a1 + 20*a0 = -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1

其他任何位都不会改变含义,并且进位到a7是“溢出”,不期望工作,所以几乎所有的算术操作都可以工作而不需要修改(正如其他人所注意到的)。符号大小通常检查符号位,并使用不同的逻辑。