Big endian machine: It thinks the first byte it reads is the biggest.
Little endian machine: It thinks the first byte it reads is the littlest.
这是linux对IP头的定义 /usr/include/linux/ip.h 或 linux/include/linux/ip.h)
版本号和首部长度是同一个字节的,这也要区分大端小端吗?我一直以为大端小端是字节间顺序的问题,不是字节内部位顺序的问题。网络数据发送时是字节流还是位流?发送时uint16_t和uint32_t的高字节必需先发送,那么同一字节的高位先发送还是低位?我找不到gcc讲结构位定义的文档,有链接么?
可以这样来解释,
1)从道理上来说,little endian中的位应该这样排列:
01234567
即排在前面的是低位。因此,先分配least significant bits
2)而在Big endian中,位应该这样排列:
76543210
即排在前面的是高位。因此,先分配most significant bits。
可以这样来理解,
1)在Big Endian的情况下,"排在前面的是高位"
a. 对于顺序的两个字节来说,第一个字节是高位(排在前面),第二个字节是低位(排在后面)。
b. 对于字节内部的位来说,
-------most significant bits排在前面,是高位,
-------least significant bits排在后面,是低位。
2)在Little Endian的情况下,"排在前面的是低位"
a. 对于顺序的两个字节来说,第一个字节是低位(排在前面),第二个字节是高位(排在后面)。
b. 对于字节内部的位来说,
-------least significant bits排在前面,是低位,
-------most significant bits排在后面,是高位。
这样,在对struct中的成员进行分配的时候,"按排列顺序分配,先分配排在前面的"
1)big endian从高位向低位分配,
a. 对字节,是先分配低地址的字节,再分配高地址的字节。
b. 对位域,先分配most significant bits,再分配least significant bits。
1)little endian从低位向高位分配,
a. 对字节,是先分配低地址的字节,再分配高地址的字节。
b. 对位域,先分配least significant bits,再分配most significant bits。
======================================
以上说的都是分配的顺序。
对于IP协议来说,
1)IP's byte order is big endian.
2)The bit endianness of IP inherits that of the CPU,
3)and the NIC takes care of converting it from/to the bit transmission/reception order on the wire.
并且,按照IP协议,
1)"version" is the most significant four bits of the first byte of an IP header.
2)"ihl" is the least significant four bits of the first byte of the IP header.
也就是说,version必须分配在most significant four bits,
按照上面说的分配顺序,在big endian中,version必须放在前面。
MSB most significant bits
LSB least significant bits
一句话:对于 little-endian 来说 MSB 在高地址,对 big-endian 来说 MSB 在低地址。
Here is how we would write the integer 0x0a0b0c0d for both big endian and little endian systems, according to the rule above:
Write Integer for Big Endian System
byte addr 0 1 2 3
bit offset 01234567 01234567 01234567 01234567
binary 00001010 00001011 00001100 00001101
hex 0a 0b 0c 0d
Write Integer for Little Endian System
byte addr 3 2 1 0
bit offset 76543210 76543210 76543210 76543210
binary 00001010 00001011 00001100 00001101
hex 0a 0b 0c 0d
In both cases above, we can read from left to right and the number is 0x0a0b0c0d.
在小字节序机器上跑测试例1:
测试例2:
原帖地址:http://www.unixresources.net/linux/clf/program/archive/00/00/64/28/642822.html
亦可参考:http://bbs.chinaunix.net/viewthread.php?tid=823662&extra=&page=1
http://www.unixresources.net/linux/clf/linuxK/archive/00/00/63/86/638637.html
Endianness of CPU
The CPU endianness is the byte and bit order in which it interprets multi-byte integers from on-chip registers, local bus, in-line cache, memory and so on.
Little endian CPUs include Intel and DEC. Big endian CPUs include Motorola 680x0, Sun Sparc and IBM (e.g., PowerPC). MIPs and ARM can be configured either way.
Endianness of Ethernet
Ethernet is big endian. This means the most significant byte of an integer field is placed at a lower wire byte address and transmitted/received in front of the least significant byte.
Endianness of IP
IP's byte order also is big endian. The bit endianness of IP inherits that of the CPU, and the NIC takes care of converting it from/to the bit transmission/reception order on the wire.
这次总算有原创的文章了,因为工作中比特顺序/字节顺序总是搞不清楚。正好昨天晚上和同事好好考论了一番,在这里总结一下。
1. 小端
在bitstream的定义中,经常见到这样的定义
exam{
A:18 bit stream left bit first
B: 14 bit stream left bit first
}
在小端(little endian)系统中,上述32bit被读入内存,memory map如下
------------------------------------------------------------------------------------
| A7 ... A0 | A15 ... A8 | B5 ... B0 A17 A16 | B13 ... B6 |