大小端

Endianness

计算机科学中描述多字节数据在内存中存储顺序的核心概念

01 | Big-Endian | Little-Endian
BIG
大端序

高位字节 → 低地址

低位字节 → 高地址

符合人类阅读习惯

0x12345678
0x1000
12
0x1001
34
0x1002
56
0x1003
78
Big-Endian 存储方式
0x1000
78
0x1001
56
0x1002
34
0x1003
12
Little-Endian 存储方式
LITTLE
小端序

低位字节 → 低地址

高位字节 → 高地址

符合计算机处理习惯

不同系统的实现

Different System Implementations

大端序系统

Big-Endian Systems

IBM 大型机
IBM Mainframes
Sun SPARC
SPARC Architecture
网络协议
Network Protocols
PowerPC (部分)
PowerPC (Some versions)

小端序系统

Little-Endian Systems

Intel x86
x86 Architecture
AMD64
AMD64 Architecture
ARM (默认)
ARM (Default mode)

混合端序

Bi-Endian Systems

ARM (可切换)
ARM (Switchable)
MIPS
MIPS Architecture
PowerPC (新版)
PowerPC (New versions)

大小端检测

Endianness Detection Methods

方法一

指针类型转换检测

bool is_little_endian() {
    unsigned int x = 1;
    return *((unsigned char *)&x) == 1;
}

通过检查整数1的最低字节来判断字节序

方法二

联合体检测方法

union {
    unsigned int i;
    unsigned char c[4];
} u;

u.i = 0x12345678;
if (u.c[0] == 0x78) {
    printf("系统是小端序\n");
} else {
    printf("系统是大端序\n");
}

使用联合体直观地观察字节存储顺序

应用场景

Practical Applications

网络通信

Network Communication

网络字节序

统一使用大端序作为标准

// 转换函数
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

文件格式

File Formats

BMP 图像
小端序
JPEG 文件
大端序
UTF-16
BE/LE 两种模式

跨平台开发

Cross-platform Development

二进制数据
Binary Data Processing
文件 I/O
File Input/Output
数据交换
Data Exchange

历史由来

Historical Origin

《格列佛游记》

"大端"和"小端"术语源自乔纳森·斯威夫特的小说《格列佛游记》。小人国的人们为了吃鸡蛋应该从大头(Big-End)还是小头(Little-End)敲开而争论不休,甚至为此发动了战争。

1980年,丹尼·科恩在论文《论大端和小端》中借用了这个典故来描述字节序。

Big-End vs Little-End

总结

Summary & Key Points

01

大端序

符合人类阅读习惯
高位字节在前

02

小端序

符合计算机处理习惯
低位字节在前

03

网络字节序

统一采用大端序
确保数据一致性

04

实际应用

网络编程、文件格式
跨平台开发

核心要点

内存存储

多字节数据的存储顺序决定了系统的字节序特性

数据转换

不同系统间通信需要进行适当的字节序转换

系统兼容

了解字节序对于底层开发和系统集成至关重要