简体   繁体   English

什么是 1 的补码加法?

[英]What is 1's complement addition?

https://www.ietf.org/rfc/rfc1071.txt https://www.ietf.org/rfc/rfc1071.txt

I see "1's complement addition" is mentioned above.我看到上面提到了“1的补码加法”。 But I don't find its official definition.但我没有找到它的官方定义。 Could anybody let me know what it is defined?谁能告诉我它的定义是什么?

In ones' complement arithmetic, the last carry bit is wrapped around and added to the result ( end-around carry ).在一个补码算术中,最后一个进位位被环绕并添加到结果中( end-around carry )。 This is also described in RFC 1071 :这也在RFC 1071中有所描述:

On a 2's complement machine, the 1's complement sum must be computed by means of an "end around carry", ie, any overflows from the most significant bits are added into the least significant bits.在 2 的补码机器上,1 的补码和必须通过“结束进位”来计算,即,任何来自最高有效位的溢出都被添加到最低有效位中。

In pseudocode, this could be implemented like this:在伪代码中,可以这样实现:

Function Sum(a: Int16, b: Int16): Int16 {
    c: Int32 := a + b
    return (c AND 0xFFFF) + (c >> 16)
}

Example例子

   F000
 + 1009
-------
   0009
 + 0001 // add carry
-------
   000A

The reason the ones' complement arithmetic is used in RFC 1071 is, I guess, that many computer systems used it at the time the Internet Checksum was developed in the 1970s.我猜,RFC 1071 中使用二进制补码算法的原因是,在 1970 年代开发 Internet 校验和时,许多计算机系统都使用了它。 This Wikipedia page describes why the end-around carry is necessary when the bits represent signed integers. Wikipedia 页面描述了为什么当位表示有符号整数时需要使用结束进位。

1's complement is a number representation where to take the negative of a number, you simply flip all the bits. 1 的补码是一个数字表示,取一个数字的负数,您只需翻转所有位。 Addition in this format would simply involve representing numbers as such, then performing binary addition.这种格式的加法只涉及表示数字,然后执行二进制加法。

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

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