one's complement sum

How to compute 16-bit one's complement sum

 

What that means is:

1. Add the 16-bit values up. Each time a carry-out (17th bit) is
produced, swing that bit around and add it back into the LSb (one's
digit). This is somewhat erroneously referred to as "one's complement
addition."

2. Once all the values are added in this manner, invert all the bits
in the result. A binary value that has all the bits of another binary
value inverted is called its "one's complement," or simply its
"complement."

For example, suppose our header has the following data. I separate the
data into groups of 4 bits only for readability. (I know that headers
are really longer than 8 bytes, but this will demonstrate the
process):

     1000 0110 0101 1110
     1010 1100 0110 0000
     0111 0001 0010 1010
     1000 0001 1011 0101

First, we add the 16-bit values 2 at a time:

         1000 0110 0101 1110   First 16-bit value
     +   1010 1100 0110 0000   Second 16-bit value
       ---------------------
       1 0011 0010 1011 1110   Produced a carry-out, which gets added
     +  /----------------> 1   back into LBb
       ---------------------
         0011 0010 1011 1111
     +   0111 0001 0010 1010   Third 16-bit value
       ---------------------
       0 1010 0011 1110 1001   No carry to swing around (**)
     +   1000 0001 1011 0101   Fourth 16-bit value
       ---------------------
       1 0010 0101 1001 1110   Produced a carry-out, which gets added
     +  /----------------> 1   back into LBb
       ---------------------
         0010 0101 1001 1111   Our "one's complement sum"

(**) Note that we could "swing around" the carry-out of 0, but adding
0 back into the LSb has no affect on the sum. (But technically, that's
what the checksum generator does.)

Of course, we'd continue to add the rest of the header data in 16-bit
segments until all data were included.

Then we have to take the one's complement of the sum. We do this by
simply inverting all the bits in the final result from above:

         0010 0101 1001 1111   Our "one's complement sum"

         1101 1010 0110 0000   The "one's complement"

So the checksum stored in the header would be 1101 1010 0110 0000.

### 原码、反码、补码、移码的加减运算规则 #### 原码的加减法 对于原码而言,符号位不参与实际的数据运算。具体来说,在执行加法操作时,如果两数同号,则绝对值相加;异号则比较大小做减法,并保留较大数的符号。而减法则转换成加法处理。 #### 反码的加减法 反码的加法相对简单一些,可以将两个数直接按位相加,但是需要注意的是如果有进位产生的话,这个进位应该再次加入到最低有效位上形成所谓的“循环进位”。至于减法,可以通过加上被减数相反数对应的反码形式来实现[^3]。 ```python def one_complement_addition(a, b): result = a ^ b # XOR operation simulates addition without carry carry = (a & b) << 1 # AND and shift left to get the carry bit(s) while carry != 0: temp_result = result result ^= carry carry = (temp_result & carry) << 1 return result def one_complement_subtraction(minuend, subtrahend): negated_subtrahend = ~subtrahend # Negate by flipping bits return one_complement_addition(minuend, negated_subtrahend) print(bin(one_complement_addition(0b0011, 0b0101))) # Example of adding two positive numbers in one's complement form. print(bin(one_complement_subtraction(0b0011, 0b0101))) # Example of subtracting using one's complement method. ``` #### 补码的加减法 在补码体系下,无论是正数还是负数都可以通过简单的二进制加法完成加法运算,即使遇到不同符号的情况也不必特殊对待。而对于减法,只需把减数变为其补码表示后再进行常规意义上的加法即可得到最终的结果。任何超出范围产生的进位都会自动丢弃掉[^1]。 ```c++ #include <iostream> using namespace std; int add(int num1, int num2){ unsigned int sum; sum = ((unsigned int)(num1)) + ((unsigned int)(num2)); return (int)((sum)&((~(((unsigned int)-1)>>1)))); } // Subtract function implemented via converting subtraction into addition with twos' complement representation int subtract(int minuend, int subtrahend){ // Convert 'subtrahend' to its negative equivalent under Two’s Complement system subtrahend = (~subtrahend)+1; return add(minuend,-subtrahend); } ``` #### 移码的加减法 由于移码本质上是对补码进行了偏置调整后的版本,因此其基本运算法则是基于补码的基础上稍作变动而来。通常情况下,先按照补码的方式来进行计算,之后再根据具体情况决定是否需要对结果施加额外的操作以恢复原始含义上的正确性。不过值得注意的一点是在某些特定场景里可能还需要考虑如何妥善处理溢出等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值