I. Overview
Byte order, Also knownClient order,Tail order, English:Endianness.
In the field of computer science,Byte orderIt refers to the order of bytes that store multi-byte data. A typical case is the storage mode of integers in the memory and the transmission sequence of network transmission. Endianness can also be usedOrder(BIT ).
The size-to-size sequence is related to the hardware architecture. All x86 series PCs are in the small-End sequence and have nothing to do with the operating system. The Solaris System on the x86 series PC is in small order, while the Solaris System on the sun or Linux platform is in large order.
Large-end byte order, high-byte storage in memory low-address, low-byte storage in memory high-address; Small-end byte order vice versa.
For example, a long data type is 0x12345678.
Large-end byte order:
Low memory address --> 0x12
0x34
0x56
High memory address --> 0x78
Small-end byte order:
Low memory address --> 0x78
0x56
0x34
High memory address --> 0x12
Ii. Large and small orders
Big client Sequence(English: Big-Endian) orBig tail order.
Data in 8 bits:
Address growth direction→ |
... |
0x0a |
0x0b |
0x0c |
0x0d |
... |
In the example, the highest valid bit (MSB, most significant byte) is0x0aStored at the lowest memory address. Next byte0x0bThere is a later address. It is similar to the reading order of hexadecimal bytes from left to right.
Data in 16 bits:
Address growth direction→ |
... |
0x0a0b |
0x0c0d |
... |
|
|
The highest 16-bit unit 0x0a0b is stored at a low level.
Small client order(English: Little-Endian) orTail order.
Data in 8 bits:
Address growth direction → |
... |
0x0d |
0x0c |
0x0b |
0x0a |
... |
The minimum valid bit (LSB, least significant byte) is0x0dStored at the lowest memory address. The next byte is followed by the address.
Data in 16 bits:
Address growth direction → |
... |
0x0c0d |
0x0a0b |
... |
|
|
Minimum 16-bit unit0x0c0dLow storage.
The CPU with large-end order and CPU with small-end order are not only in bytes, but also in BIT order.
For example, 0x01 is stored in memory.
Large-End sequence: Memory low bit: 00000001 memory high bit
Small-End sequence: Memory low bit: 10000000 memory high bit
For example, 0x00000001
Large-End sequence: Memory Low Bit 00000000 00000000 00000000 00000001 memory high bit
Small-End sequence: Memory Low Bit 10000000 00000000 00000000 00000000 memory high bit
Iii. DETERMINATION METHOD
Typedef struct tagregion {unsigned char region1: 1; unsigned char region2: 6; unsigned char region3: 1;} region; Union {region BB; unsigned char AA;} cc; int main (INT argc, char * argv []) {CC. bb. region1 = 1; printf ("Hello world! % D/N ", CC. aa); // print 1 indicates a small end, and print 128 indicates a large end. Return 0 ;}
static unsigned char isBigEndian(){ const unsigned short test= 0xFF00; return *((unsigned char *) &test);};
The following methods are more concise:
1 #include <iostream>
2 using namespace std;
3
4 int main(int argc, char **argv)
5 {
6 short s = 0x0102;
7 if((*(unsigned char*)&s) == 2)
8 cout<<"little endian"<<endl;
9 else if((*(unsigned char*)&s) == 1)
10 cout<<"big endian"<<endl;
11 else
12 cout<<"unknown endian\n"<<endl;
13
14 return 0;
15 }