What are big endian and little endian?

Source: Internet
Author: User
Tags htons
What are big endian and little endian?

We generally translate endian into byte order, and call Big endian and little endian "Big tail" and "Small Tail ".

2. What are big endian and little endian?

When designing a computer system, there are two methods to process data in the memory. It is called Little-Endian. The value of the percentile bit in the memory is the rightmost part of the data (that is, the percentile part of the data ). For example, if a hexadecimal number is 0x12345678, the memory is stored as follows:

Value

0111,1000

0101,0110

0001,0010

Address

100

101

102

103

The other is called Big-Endian. On the contrary, the value of the percentile bit stored in the memory comes from the leftmost side of the data (that is, the highest part of the data ). For example, if a hexadecimal number is 0x12345678, the memory is stored as follows:

Value

0001,0010

0101,0110

0111,1000

Address

100

101

102

103

For example, some files must be processed on different platforms, orSocket communication. In this regard, we can use ntohl (), ntohs (), htonl (), and htons () functions to convert the format.

3. How can we determine whether the system is big endian or little endian?

Search for the byte_order (or _ byte_order, _ byte_order) string in/usr/include/(including sub-Directories) and determine its value. This value can be found in the endian. h or machine/endian. h file. Sometimes in feature. H, differentOperating SystemIt may be different. In general, the byte_order (or _ byte_order ,__ byte_order) of the little endian system is 1234, and the big endian system is 4321. Most users have little endian operating systems (such as Windows, FreeBSD, and Linux. A small portion, such as Mac OS, is big endian. Essentially, either little endian or big endian is related to the operating system and chip type.

========================================================== ====================================

Ext3 File System Data in hard disk partitions is stored in Intel's little-Endian format. If ext3 data is developed on platforms other than PCsProgramPay special attention to this.

Speaking of the byte sequence, there must be two major CPU factions. That is, Motorola's PowerPC series CPU and Intel's X86 series CPU. The PowerPC series uses the big endian method to store data, while the x86 series uses the little endian method to store data. So what is big endian and little endian?

In fact, big endian is used to store the highest valid byte (MSB) at a low address, while little endian is used to store the lowest valid byte (LSB) at a low address ).

Text descriptions may be abstract. The following uses images to describe them. For example, the storage Order of the number 0x12345678 in two different bytes of CPU is as follows:

big endian

low address and high address
--------------------------------------->
+-+ -+
| 12 | 34 | 56 | 78 |
+-+

Little endian

Low address and high address
----------------------------------------->
+-+
| 78 | 56 | 34 | 12 |
+-+

From the above two figures, we can see that using the big endian method to store data is in line with our human thinking habits. And little endian ,! @ # $ % ^ & *, Go to hell-_-|

Why should we pay attention to the issue of byte order? You may ask this question. Of course, if the program you write runs only in a single-host environment and does not deal with other programs, you can ignore the existence of the byte sequence. But what if your program needs to interact with other programs? Here I want to talk about two languages. In C/C ++ programming, the data storage sequence is related to the CPU of the compilation platform, while in Java programming, the only way to store data is big endian. Imagine what will happen if you use a program written on the x86 Platform in C/C ++ to communicate with other Java programs? Take the above 0x12345678 as an example. The pointer pointing to 0x12345678 is passed to the Java program, because Java uses the big endian method to store data, naturally, it translates your data into 0x78563412. What? Is it actually another number? Yes, that's the consequence. Therefore, it is necessary to convert the byte order before your C program passes it to the Java program.Work.

Coincidentally, all network protocols use big endian to transmit data. So sometimes we call the big endian method the byte order of the network. When two hosts communicate in different bytes, data must be converted to network bytes before transmission.

 

Different CPUs have different sort of bytes. These sort of bytes refer to the order in which integers are stored in the memory. This is called the host order.
There are two most common
1. little endian: stores low-order bytes at the starting address
2. Big endian: stores High-Order bytes at the starting address.

Le little-Endian
The byte sequence that best fits people's thinking
Low-level address storage value
High address storage value
This is the byte sequence that best fits people's thinking, because it is from the perspective of human first.
If the low value is small, it should be placed where the memory address is small, that is, the low value of the memory address.
Otherwise, the high value should be placed in the place where the memory address is large, that is, the memory address is high.

Be big-Endian
The most intuitive byte order
High level of the low-level address storage value
The low storage value of the high address
Why is it intuitive? Do not consider mappings.
Write the memory address from left to right in ascending order.
Write the value in the order of high to low.
By contrast, one byte and one byte are filled in.

Example: Memory dual-word 0x01020304 (DWORD) Storage Method

Memory Address
4000 4001 4002 4003
Le 04 03 02 01
Be 01 02 03 04

Example: If we write 0x1234abcd to the memory starting with 0x0000, the result is
Big-Endian little-Endian
0x0000 0x12 0xcd
0x0001 0x23 0xab
0x0002 0xab 0x34
0x0003 0xcd 0x12
X86 series CPUs are in the byte order of little-Endian.

The Network byte sequence is a data representation format specified in TCP/IP. It has nothing to do with the specific CPU type and operating system, this ensures that data can be correctly interpreted during transmission between different hosts. The Network byte sequence adopts the big endian sorting method.

The following four conversion functions are provided for BSD socket conversion:
Htons converts the unsigned short type from host to Network
Htonl converts the unsigned long type from the host sequence to the network Sequence
Ntohs converts the unsigned short type from the network sequence to the host Sequence
Ntohl converts the unsigned long type from the network sequence to the host Sequence

In systems using little endian, these functions convert the byte order.
In systems using the big endian type, these functions are defined as empty macros.

During network program development or cross-platform development, you should also ensure that only one byte sequence is used. Otherwise, different interpretations of the two parties may cause bugs.

Note:
1. Network and host byte Conversion Function: htons ntohs htonl ntohl (s means that short L is long H is host N is network)
2. different operating systems run on different CPUs in different bytes. See the following table.
Processor operating system byte sorting
Alpha all little endian
HP-PA nt little endian
UNIX big endian HP-PA
Intelx86 all little endian <----- x86 systems are small-end bytecode Systems
Motorola680x () All big endian
MIPs nt little endian
MIPs UNIX big endian
PowerPC nt little endian
PowerPC non-nt big endian <----- PPC system is a large-end bytecode System
RS/6000 UNIX big endian
Linux UNIX big endian
IXP1200 ARM core all little endian

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.