我只是好奇,为了在二进制中表示-1,是否有一个原因,使用了2的补码:翻转位并加1?
-1由11111111(2的补码)表示,而不是(对我来说更直观)10000001,10000001是二进制1,第一位为负标志。
免责声明:我的工作不依赖二进制算术!
我只是好奇,为了在二进制中表示-1,是否有一个原因,使用了2的补码:翻转位并加1?
-1由11111111(2的补码)表示,而不是(对我来说更直观)10000001,10000001是二进制1,第一位为负标志。
免责声明:我的工作不依赖二进制算术!
当前回答
2的补数允许以正常的方式进行加减法(就像对无符号数字进行绕线运算一样)。它还防止了-0(一种单独的表示0的方法,如果使用常规的逐位比较数字的方法,它将不等于0)。
其他回答
A major advantage of two's-complement representation which hasn't yet been mentioned here is that the lower bits of a two's-complement sum, difference, or product are dependent only upon the corresponding bits of the operands. The reason that the 8 bit signed value for -1 is 11111111 is that subtracting any integer whose lowest 8 bits are 00000001 from any other integer whose lowest 8 bits are 0000000 will yield an integer whose lowest 8 bits are 11111111. Mathematically, the value -1 would be an infinite string of 1's, but all values within the range of a particular integer type will either be all 1's or all 0's past a certain point, so it's convenient for computers to "sign-extend" the most significant bit of a number as though it represented an infinite number of 1's or 0's.
Two's-complement is just about the only signed-number representation that works well when dealing with types larger than a binary machine's natural word size, since when performing addition or subtraction, code can fetch the lowest chunk of each operand, compute the lowest chunk of the result, and store that, then load the next chunk of each operand, compute the next chunk of the result, and store that, etc. Thus, even a processor which requires all additions and subtractions to go through a single 8-bit register can handle 32-bit signed numbers reasonably efficiently (slower than with a 32-bit register, of course, but still workable).
当使用C标准所允许的任何其他有符号表示时,结果的每一位都可能受到操作数的任何位的影响,这就需要将整个值一次保存在寄存器中,或者在计算之后进行额外的步骤,至少在某些情况下,需要读取、修改和重写结果的每个块。
为什么用Two2的补语系统来表示负数,而不是用One的补语系统,一个令人满意的答案是 二的补语系统解决了一的补语系统中存在的表示负数的0的多重表示和对进位的需要。
欲了解更多信息,请访问https://en.wikipedia.org/wiki/Signed_number_representations
用于末端绕行访问 https://en.wikipedia.org/wiki/End-around_carry
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.
我有一个在某些情况下很重要的小补充:在这些限制条件下,2的恭维是唯一可能的表示:
无符号数和二的补数是恒等交换环。它们之间有一个同态。 它们共享相同的表示,对负数有不同的分支切割,(因此,为什么它们之间的加法和乘法是相同的)。 高位决定符号。
要知道为什么,它有助于降低基数;例如Z_4。
星座、星等和一个人的赞美都不能形成一个具有相同数量元素的环;一个症状是双零。因此,很难在边缘上工作;为了在数学上保持一致,它们需要检查溢出或陷阱表示。
你的目的并不是把二进制数的所有位都颠倒过来。实际上就是用1减去每一位。这只是一个幸运的巧合,1减1得0,1减0得1。所以翻转位有效地执行了这个减法。
但为什么每个数字的差值都是1呢?你不是。您的实际目的是计算给定的二进制数与另一个具有相同位数但只包含1的二进制数的差值。例如,如果您的数字是10110001,当您翻转所有这些位时,您实际上是在计算(11111111 - 10110001)。
这解释了计算二的补的第一步。现在让我们在图中加入第二步——添加1。
将上述二进制方程加1:
11111111-10110001 + 1
你得到了什么?这样的:
100000000-10110001
这是最终方程。通过执行这两个步骤,你试图找到这个,最终的区别:二进制数减去另一个二进制数,多出一位,并且除最高位外都包含零。
但我们为什么要追求这种差异呢?好吧,从现在开始,我想你最好去读维基百科的文章。