永州网,内容丰富有趣,生活中的好帮手!
永州网 > > 正文

Compute 16-bit One's Complement Sum

时间:1999-12-03 00:03:25

相关推荐

Compute 16-bit One's Complement Sum

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.- Doctor TWE, The Math Forumhttp://mathforum.org/dr.math/ Date: 08/16/2002 at 08:51:24From: Arthur SheimanSubject: How to compute 16-bit one"s complement sumHi Dr. Math,This article on computing the IP header checksum using the complement of the "one"s complement addition" is very good and 100% technically correct, but I think it would be improved with another paragraph at the end, as follows.In practice, the efficiency of computing the IP header checksum is very important. When computing the checksum in software on a 32-bit processor, e.g., Intel 386 and above, it is more efficient to first add up all the numbers using 32-bit arithmetic, and then add the overflow at the end. This may result in another 16-bit overflow, so this last step needs to be done twice. Three times is not necessary, as it cannot overflow again (FFFF + FFFF = 1FFFE; FFFE + 0001 = FFFF).Another important practical issue is the endian (byte order) of the machine. Suggested reading is RFC1071, available free from www.ietf.org, especially section 3, which demonstrates computation on both the world"s most popular microprocessor, Intel, and less popular machines, e.g., big endian.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
中德生态城2周年:区域发展高峰论坛圆满落幕

中德生态城2周年:区域发展高峰论坛圆满落幕

来自中德两国的政府官员、行业领袖、学术专家和企业代表齐聚一堂,分享关于生态城建设和区域发展的最新理念和实践经验

2024-02-03

马虎大意!忘记戴罩子竟然吃药一整天 奇迹般安然无恙?

马虎大意!忘记戴罩子竟然吃药一整天 奇迹般安然无恙?

他们将能够根据你儿子所服用的药物类型和剂量,评估可能的风险并提供适当的指导

2023-12-17

1996年深夜时分最扎心的歌:感动无数人的泪水

1996年深夜时分最扎心的歌:感动无数人的泪水

这首歌具有强烈的情感共鸣,歌词充满对爱情的思念和无奈

2024-02-10

小学三年级生写的300字端午节作文:我和家人一起包粽子 赛龙舟 听屈原故事

小学三年级生写的300字端午节作文:我和家人一起包粽子 赛龙舟 听屈原故事

文章首先介绍了端午节的起源和传统习俗,然后描述了小作者一家人包粽子、挂艾草的情景,以及赛龙舟的欢乐气氛

2024-02-10