1's complement and 2's complement Representation
来源:互联网 发布:关于网络的作文 高中 编辑:程序博客网 时间:2024/05/18 12:33
一的补码(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)
BinaryDecimalHex0000 00000000000 00011010000 00102020000 00113031111 1111-1FF1111 1110-2FE1111 1101-3FDLet'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)
BinaryDecimalHex0000 00000000000 00011010000 00102020000 00113031111 1111-0FF1111 1110-1FE1111 1101-2FD1111 1100-3FCAdd 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
- 二补数 (2's complement)
- one's complement & two's complement
- one's complement & two's complement
- Two's Complement
- one's complement sum
- one's-complement 反码, two's-complement 补码, one's complement sum, two's complement sum
- TWO’S COMPLEMENT NOTATION
- Two's complement
- Two's Complement
- [总结] Two's complement
- ones' complement 和 two's complement
- 1的补码 2的补码 1's complement
- Part 2 of 2 Two‘s complement
- one's complement & two's complement【待考究】
- YP.2.2 Integer Data Types and 2.3 2's Complement Integers(双语)
- 终于明白 2’s complement 乘法是怎么做的了!
- 负数补码(two’s complement)的原理及证明
- 结构体以及共用体与枚举类型
- EF资料链接
- 黑马程序员_Java高新技术
- 黑马程序员_网络编程
- uva 409 Excuses, Excuses!
- 1's complement and 2's complement Representation
- java 接口 实现和继承关系
- Sprite Kit Actions(一)
- 4.18 st_atime、st_mtime和st_ctime成员-文件时间属性
- 什么时候使用接口?什么时候使用抽象?选择Java接口还是抽象类
- 权限
- sed命令
- 爬虫(一)---爬行算法
- 爬虫(二)-关于单体爬虫的设计问题