打印

普及教育知识

[复制链接]
1210|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
6019赵文|  楼主 | 2011-3-9 13:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
LEM, COM, TI, ck, TE
一的补码(one's complement) 指的是正数=原码,负数=反码
而二的补码(two's complement) 指的就是通常所指的补码
IP checksum definition
The IP checksum is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header.
One question many people may ask is "What is the 1's complement sum ?". This is because all computers utilize the 2's complement representation and the 1's complement is not used. The following gives a short introduction.
2's complement fixed point integers (8-bit)
BinaryDecimalHex
0000 0000000
0000 0001101
0000 0010202
0000 0011303
1111 1111-1FF
1111 1110-2FE
1111 1101-3FD

Let's add two intergers:
-3 + 5 = 2
FD + 05 = 01 02
Discarding the carry (01) gives the correct result.
1's complement fixed point integers (8-bit)
BinaryDecimalHex
0000 0000000
0000 0001101
0000 0010202
0000 0011303
1111 1111-0FF
1111 1110-1FE
1111 1101-2FD
1111 1100-3FC

Add the same numbers:
-3 + 5 = 2
FC + 05 = 01 01
Adding the carry (01) to the LSB (01) gives the correct result:
01 + 01 = 02
So, the 1's complement sum is done by summing the numbers and adding the carry (or carries) to the result..
Simple Internet checksum example

Suppose we have an 8-bit, 2's complement, machine and send the packet
FE 05 00
where 00 is the checksum field.
Let's calculate and verify the Internet checksum.
FE + 05  =  01 03
This is the result of the normal (2's complement) addition. The 1's complement sum requires the addition of the carry to the 8-bit word (even though we will not get the same result)
03 + 01 = 04   
so the 1's complement sum of FE + 05 is 04.
The 1's complement of the 1's complement sum (Internet checksum) will be  
~04  = FB
and the packet will be sent as
FE 05 FB  
Now, at the receiving end we add all the received bytes, including the checksum (again using the 2's complement representation)
FE + 05 + FB  = 01 FE   
The 1's complement sum is   
FE + 01 = FF = -0   
which checks that the transmission was OK (see below).

相关帖子

沙发
金鱼木鱼| | 2011-3-9 13:31 | 只看该作者
so the 1's complement sum of FE + 05 is 04.
The 1's complement of the 1's complement sum (Internet checksum) will be  
~04  = FB
????????

此时04是正数,  还需要取反?

使用特权

评论回复
板凳
金鱼木鱼| | 2011-3-9 13:51 | 只看该作者
某个二进制数的“1的补码”,就是对该数的各位取反;

某个二进制数的“2的补码”,就是对该数的各位取反,然后 “+ 1”


楼主你错了

使用特权

评论回复
地板
Winston.W| | 2011-3-9 14:14 | 只看该作者
非常感谢

使用特权

评论回复
5
yybj| | 2011-3-9 16:54 | 只看该作者
普及还用英文

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

350

主题

1515

帖子

1

粉丝