一的补码(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)

Binary Decimal Hex
0000 0000 0 00
0000 0001 1 01
0000 0010 2 02
0000 0011 3 03
1111 1111 -1 FF
1111 1110 -2 FE
1111 1101 -3 FD

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)

Binary Decimal Hex
0000 0000 0 00
0000 0001 1 01
0000 0010 2 02
0000 0011 3 03
1111 1111 -0 FF
1111 1110 -1 FE
1111 1101 -2 FD
1111 1100 -3 FC

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).

A more complex example (32-bit machine)

As shown in RFC 1071, the checksum calculation is done in the following way:

(1) Adjacent octets to be checksummed are paired to form 16-bit integers, and the 1's complement sum of these 16-bit integers is formed.

(2) To generate a checksum, the checksum field itself is cleared, the 16-bit 1's complement sum is computed over the octets concerned, and the 1's complement of this sum is placed in the checksum field.

(3) To check a checksum, the 1's complement sum is computed over the same set of octets, including the checksum field. If the result is all 1 bits (-0 in 1's complement arithmetic), the check succeeds.

Packet

01 00 F2 03 F4 F5 F6 F7 00 00

(00 00 is the checksum field)

Form the 16-bit words

0100 F203 F4F5 F6F7

Calculate 2's complement sum

0100 + F203 + F4F5 + F6F7 = 0002 DEEF (store the sum in a 32-bit word)

Add the carries (0002) to get the 16-bit 1's complement sum

DEEF + 002 = DEF1

Calculate 1's complement of the 1's complement sum

~DEF1 = 210E

We send the packet including the checksum 21 0E

01 00 F2 03 F4 F5 F6 F7 21 0E

At the receiving

0100 + F203 + F4F5 + F6F7 + 210E = 0002 FFFD
FFFD + 0002 = FFFF

which checks OK.

Comments
It may look awkword to use a 1's complement addition on 2's complement machines. This method however has its own benefits.

Probably the most important is that it is endian independent. Little Endian computers store hex numbers with the LSB last (Intel processors for example). Big Endian computers put the LSB first (IBM mainframes for example). When carry is added to the LSB to form the 1's complement sum (see the example) it doesn't matter if we add 03 + 01 or 01 + 03. The result is the same.

Other benefits include the easiness of checking the transmission and the checksum calculation plus a variety of ways to speed up the calculation by updating only IP fields that have changed.

The IP Header Checksum is computed on the header fields only. 
Before starting the calculation, the checksum fields (octets 11 and 12) 
are made equal to zero.

In the example code, 
u16 buff[] is an array containing all octets in the header with octets 11 and 12 equal to zero. 
u16 len_ip_header is the length (number of octets) of the header.

/*
**************************************************************************
Function: ip_sum_calc
Description: Calculate the 16 bit IP sum.
***************************************************************************
*/
typedef unsigned short u16;
typedef unsigned long u32;

u16 ip_sum_calc(u16 len_ip_header, u16 buff[])
{
        u16 word16;
        u32 sum=0;
        u16 i;
    
        // make 16 bit words out of every two adjacent 8 bit words in the packet
        // and add them up
        for (i=0;i<len_ip_header;i=i+2){
                word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
                sum = sum + (u32) word16;
        }

// take only 16 bits out of the 32 bit sum and add up the carries
        while (sum>>16)
                sum = (sum & 0xFFFF)+(sum >> 16);

// one's complement the result
        sum = ~sum;

return ((u16) sum);
}

1's complement and 2's complement Representation相关推荐

  1. What’s difference between 1’s Complement and 2’s Complement?

    1's complement of a binary number is another binary number obtained by toggling all bits in it, i.e. ...

  2. ones' complement 和 two's complement

    ones' complement    译为"一的补码",或译为"反码". two's complement     译为"二的补码",或译 ...

  3. one's-complement 反码, two's-complement 补码, one's complement sum, two's complement sum

    1.one's-complement: 反码,高位为符号位: two's-complement: 补码,高位为符号位: 2. one's complement sum 反码加法,需要加上进位: two ...

  4. [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. 整数二进制补码的数学原理(two's complement)

    转载自整数二进制补码的数学原理(two's complement) ================================================================== ...

  6. 计算机导论简答芯片,吉大计算机 - 计算机导论简答题 (2011级)

    C1 2. Q: What model is the basis for today's computers? A: Von Neumann Model. 5. Q: What are the sub ...

  7. 二进制文件签名_二进制数的签名表示

    二进制文件签名 Prerequisite: Number systems 先决条件: 数字系统 Until now, we have only talked about positive number ...

  8. 软件工程计算机导论试题及答案,2010级计算机学院计算机导论试题A卷.doc

    2010级计算机学院计算机导论试题A卷,计算机导论试题,计算机导论考试试题,护理学导论试题及答案,软件工程导论期末试题,社区护理导论试题,软件工程导论试题,药学导论试题,公共政策导论试题,语言学导论期 ...

  9. 华南理工大学计算机科学概论试卷,华南理工大学计算机概论试卷b.doc

    华南理工大学计算机概论试卷b = 1 \* ROMAN I. Multiple-Choice Questions (1.5*20=30 points) 1. What is a unique char ...

最新文章

  1. AppStore 拒绝审核原因:PLA 2.3
  2. 老夫疗法少年狂:微软中国CTO黎江:不要给区块链贴标签,也不要非黑即白区分谁是好人谁是坏人
  3. python批量生成文件夹_python实现批量获取指定文件夹下的所有文件的厂
  4. 其他脚本与 asp.net 脚本一起验证时容易出的问题
  5. 人物志 | KDD Cup 2017双料冠军燕鹏
  6. 深度学习可解释性资料整合
  7. QtCreator中导入“.lib(.a)”和“.dll(.so)”文件的方法
  8. MyBatis注解-动态SQL 一个 SqlProvider的demo
  9. 3S基础知识:VB中利用MapX创建用户定制工具
  10. Python三种设计模式
  11. 自己上手搭建VUE项目
  12. 正面管教读书笔记 10 你的性格对孩子性格的影响
  13. a java exception has occured java
  14. PL/0编译器(java version)–Pcode.java
  15. .md文件转.pdf文件
  16. 计算机网络知识点总结(第四章 网络层)
  17. 我们请来了2017 NIPS大会发文数全球前3的华人教授,讲解网络数据的表征学习(视频+PPT)
  18. vue+海康威视视频插件坑点记录
  19. Android之Fragment应用——一个简易版的新闻应用
  20. Unity3D状态机运行状态不显示解决方案哈哈哈

热门文章

  1. 【系统收藏——德邦证券同花顺 官方版】
  2. 转贴:婴儿潮”只是一个真实的谎言。
  3. 一刷代码随想录完结篇
  4. Istio服务网格中的XDS通信
  5. as.matrix报错
  6. nginx https代理http ,以及代理外网
  7. FTP匿名登录和FTP常用命令以及ftp状态码
  8. Fortran 停止进入调试代码
  9. 【首尔大学韩国语】十九课 生日
  10. html添加添加只读属性,JavaScript如何将readonly属性添加到input标签