Compute 16-bit One's Complement Sum (memo)

来源:互联网 发布:c语言编写有窗口的程序 编辑:程序博客网 时间:2024/06/04 01:08

Refer to http://mathforum.org/library/drmath/view/54379.html and http://en.wikipedia.org/wiki/Header_checksum

Compute 16-bit One's Complement Sum

Date: 03/20/2002 at 13:54:13From: JohnSubject: How to compute 16-bit one's complement sumI would like to know how to compute one's complement sum. In my TCP/IP book it says, "to compute the IP checksum for outgoing datagram, the value of checksum field is set to zero, then the 16-bit one's complement sum of the header is calculated (i.e. the entire header is considered a sequence of 16-bit words)" - but I do not know how to compute the one's complement sum. Please help.

Date: 03/21/2002 at 01:27:19From: Doctor TweSubject: Re: How to compute 16-bit one's complement sumHi John - thanks for writing to Dr. Math.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 0101First, 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.I hope this helps. If you have any more questions, write back.

Refer to http://mathforum.org/library/drmath/view/54379.html and http://en.wikipedia.org/wiki/Header_checksum
0 0