Big endian and little endian

Source: Internet
Author: User

I. byte order

From: http://ayazh.gjjblog.com/archives/1058846/

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 on to the Java program.
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. Ansi c provides the following four macros for converting the byte order.

Big endian: the highest byte is in the second bit of the address, and the lowest byte is in the highest bit of the address, which is arranged in sequence. Little endian: the lowest byte is in the lowest Bit, the highest byte is in the highest bit, and is arranged in reverse order.

Endian refers to the arrangement of logical to physical units when the minimum physical unit is smaller than the minimum logical unit hour. The minimum physical unit we come into contact with is byte. In the field of communication, it is often bit, but the principle is similar.

Example: If we write 0x1234abcd to the memory starting with 0x0000, the result is big-Endian little-Endian 0x0000 0x12 0xcd 0x0001 0x34 0xab 0x0002 0xab 0x34 0x0003 0xcd 0x12.
Currently, little endian is the mainstream, because address issues are not considered during data type conversion (especially pointer conversion.

 

Ii. Big endianAnd little endianThe origin of nouns

These two terms come from Jonathan Swift's "garifo Travelogue", where the two factions involved cannot reach an agreement on which end-small or big-end-should open a half-cooked egg. :)

The word "endian" comes from Gulliver Travel Notes. The civil war in the minor people's country originated from the fact that the big-Endian attack or the little-Endian attack were initiated when the eggs were eaten. As a result, there were six rebels, one of the emperors gave life and the other lost the throne.

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

In that era, Swift was a constant conflict between Britain and France. Danny Cohen, an early pioneer in network protocols, used these two terms for the first time to refer to the byte sequence, later, this term was widely accepted.

 

3. Big endianAnd little endianAdvantages and disadvantages

From: dr. William T. Verts, limit l 19,199 6

Big endian

It is easy to determine the positive and negative values of a number. Only one byte at offset0 can be used for confirmation.

Little endian

It can be 1, 2, and 4 bytes in length. The data type conversion is very convenient.

 

Iv. Collation of some common files

From: dr. William T. Verts, limit l 19,199 6

 

Common file formats and Their endian order are as follows:

  • Adobe Photoshop-- Big endian
  • BMP (Windows and OS/2 bitmaps)-- Little endian
  • DXF (AutoCAD)-- Variable
  • GIF-- Little endian
  • IMG (GEM raster)-- Big endian
  • JPEG-- Big endian
  • Fli (Autodesk animator)-- Little endian
  • Macpaint-- Big endian
  • PCX (PC paintbrush)-- Little endian
  • Postscript-- Not applicable (Text !)
  • POV (persistence of vision ray-tracer)-- Not applicable (Text !)
  • Qtm (QuickTime movies)-- Little endian (on a Mac !) (Peterlee note big endian in my opinion)
  • Microsoft riff (. wav &. AVI)-- Both
  • Microsoft RTF (Rich Text Format)-- Little endian
  • SGI (Silicon Graphics)-- Big endian
  • Sun raster-- Big endian
  • TGA (Targa)-- Little endian
  • Tiff-- Both, endian identifier encoded into File
  • WPG (WordPerfect Graphics Metafile)-- Big endian (on a PC !)
  • Xwd (X Window dump)-- Both, endian identifier encoded into File

 

V. Bit order

From: http://ayazh.gjjblog.com/archives/1058846/

I talked about the byte sequence in the big endian and little endian articles in August 9. However, some may still ask if the order between the eight bits in one byte is also divided into big endian and little endian when the CPU stores data in one byte? Or is there any difference in the bit order?
In fact, this bit sequence also exists. The following figure shows the number 0xb4 (10110100.
Big endian

Msb lsb ----------------------------------------------> +--+- +-+ | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | +- +-+
Little endian

Lsb msb --------------------------------------------> +--+- +-+ | 0 | 0 | 1 | 0 | 1 | 0 | 1 | +- +-+
In fact, because the minimum unit of CPU data storage operations is one byte, the bit order inside the CPU is a black box for our program. That is to say, you give me a pointer pointing to the number 0xb4. For a CPU in the big endian mode, it reads 8 bits from left to right; for a CPU in the little endian mode, the opposite is to read 8 bits of this number from right to left. Our program obtains 0xb4 after accessing through this pointer. The bit order inside the byte is invisible to the program, in fact, this is the same for the byte order on a single machine.
Someone may ask, what if it is network transmission? Will there be problems? Is it necessary to use any function to convert the bit order? Well, this is a good question. Assume that the CPU in little endian mode is transferred to the big endian mode CPU in one byte, and the 8-bit number will be read locally before transmission, then, the eight bits are transmitted in the byte order of the network, so that there will be no problems at the receiving end. If you want to transmit a 32-bit number, because the number occupies 4 bytes in littel endian storage, network transmission is carried out in bytes, the CPU of the little endian side reads the first byte and sends it. In fact, this byte is the original number of LSB. When it reaches the receiver, it becomes MSB, causing confusion.

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.