Big endian and little endian
[Introduction to big endian and little endian]
Byte endian refers to the organization of bytes in the memory, so it is also called byte ordering or byte order.
For an object that spans multiple bytes in data, we must establish such a convention for it:
(1) What is its address?
(2) How are its bytes organized in memory?
For the first question, we have the following explanation:
For objects that span multiple bytes, generally the bytes it occupies are continuous, and its address is equal to the lowest address it occupies. (The linked list may be an exception, but the address of the linked list can be seen as the address of the linked list header ).
For example, if int X is used, its address is 0x100. So it occupies the four bytes in the memory: ox100, 0x101, 0x102, and 0x103 (32-bit system, so int occupies 4 bytes ).
The above is only the case of memory byte organization: There are two conventions for the organization of multi-byte objects in the memory. Consider a W-bit integer.
The expressions of it are as follows: [Xw-1, Xw-2,..., X1, x0], its
MSB (most significant byte, maximum valid bytes) is [Xw-1, Xw-2,... Xw-8];
LSB (least significant byte, the lowest valid byte) is [X7, X6,..., x0].
The remaining bytes are located between MSB and LSB.
Who is the lowest address in the memory of LSB and MSB, that is, who represents the address of the object?
This leads to the problems of big endian and little endian.
If the LSB is in front of msb and the lsb is a low address, the machine is a small end; otherwise, the machine is a large end.
Dec (Digital Equipment Corporation, now part of Compaq) and Intel machines (x86 platforms) generally use small terminals.
IBM, Motorola (Power PC), and Sun generally use big ends.
Of course, this does not represent all situations. Some CPUs can work on small ends and big ends, such as arm, Alpha, and Motorola PowerPC. For details, refer to the processor manual.
Whether the CPU is large or small depends on the specific settings.
(For example, Power PC supports little-Endian, but the default value is big-Endian)
Generally, most users' operating systems (such as Windows, FreeBSD, and Linux) are little endian. A small portion, such as Mac OS, is big endian.
Therefore, either little endian or big endian is related to the operating system and chip type.
In Linux, you can find the byte_order (or
_ Byte_order, _ byte_order) to determine its value. Byte_order is called byte order. This value can be found in the endian. h or machine/endian. h file. Sometimes in feature. H, different operating systems may be different.
For a number 0x1122
When using the little endian method, the low-byte storage is 0x22, and the high-byte storage is 0x11.
In the big endian mode, the low-byte storage is 0x11, and the high-byte storage is 0x22.
The above description was inaccurate only after a netizen corrected it.
After thinking about it, I think the following description may be more appropriate:
When using the little endian method to store data, the LSB of the data is the least significant data bit, which is stored in the low address location. Here, the LSB is 22. That is,
Low address storage 0x22, high address storage 0x11
When using the big endian method to store data, the most meaningful data bit of MSB is stored in the low address location. Here, MSB is 11, that is
Low address storage 0x11, high address storage 0x22
Note:
1) The so-called MSB (most significant byte) name is very complicated. I don't know if anyone understands it. I was confused when I started to see this word, but I didn't fully understand it. in fact, MSB is the most important person in a number,
For example, 12004, a Chinese reader, twelve thousand and four, that is, 1 of the highest digit represents 10 thousand. Here it is called MSB, the most meaningful bit.
2) Common Data Storage: When written in text, the content writing format is mostly from low address to high address.
For example, a hexadecimal number is 0x11 22 33, and the storage location is
Address 0x3000 storage 11
Address 0x3001: 22
Address 0x3002: 33
The connection address 0x112233 is stored in 0x3000-0x3002.
This storage and Representation Method exactly matches the big end.
The explanation is a bit messy. I hope someone can understand it.
If there is any error, please correct it. Thank you.
[Use a function to determine whether the system is big endian or little endian]
Bool isbig_endian ()
// If the byte order is big-Endian, true is returned;
// If the value is little-Endian, false is returned.
{
Unsigned short test = 0x1122;
If (* (unsigned char *) & Test) = 0x11)
Return true;
Else
Return false;
} // Isbig_endian ()
The above content is organized from:
How can we determine whether the system is big endian or little endian?
Http://jlingmei.spaces.live.com/blog/cns! 77254ccc13222c11! 391. Entry? WA = wsignin1.0
Determine whether the machine's byte storage sequence is big endian or little endian
Http://hi.baidu.com/cppyun/blog/item/9625c8396d5ff7f33b87ce33.html
Dynamically determines whether the CPU's byte order is big-Endian or little-Endian.
Http://chongyanglee.bokee.com/4919503.html