What is little endian mode and what is big endian mode

Insert image description here

Endianness

Preface

When memory reads and writes data, it reads and writes in bytes, and its smallest read and write unit is also bytes. One byte occupies 8 bits. If we only consider unsigned numbers for the time being, then the range it can represent is only 256 integer values ​​0 ~ 255. If a byte of data is stored in memory, then the read and write operations on this byte of data will be very good. However, the fact is that data in real life is often larger than one byte. For example, in our commonly used PCs (32 bits or 64-bit), there are very few data types that occupy one byte. The common ones are char and int, which occupy 4 bytes, and double occupies 8 bytes. For this reason, to store data larger than one byte, multiple bytes have to be used to store it. Now the question arises, how to read and write data larger than one byte? What about?

What is endianness?

We now know that the storage of data larger than one byte in memory must be larger than one byte. However, as the so-called solution to the multi-byte storage problem introduces a new problem, that is, How to read and write multiple bytes of data?

This introduces the byte order. Byte order, as the name suggests, is byte order.

Now let's think about it. If we have a two-byte integer data stored in the memory, what is the biggest problem when we read this two-byte data?

I would like to add here that as just mentioned, the read and write operations of memory data are all in bytes, that is, operations one by one, and from the low address of the memory to the high address. directional. There are two basic operations when reading memory data, one is "random" and the other is " Linear”. "Random" means that the memory is a random access memory, which is what we often call RAM (Random Access Memory), so it can jump to the memory Read the stored data at any location (this is also one of the reasons why the access speed of our memory stick is much faster than that of ROM). "Linear" What it means is that when reading memory data, you have to read from the starting address of random reading from the low address to the high address like a straight line.

Then when we read this two-byte data,the biggest problem is: the byte we are currently reading is this two-byte Is the data high or low?

Assume that the hexadecimal value of this two-byte integer data is: 0x1234. The byte we are reading now is 0x12, so how do you know whether this 0x12 is its high byte or its low byte? If you are not sure about this, then when reading and writing data, They are definitely all wrong.
In other words, Should the low byte of data be stored at the high address or the low address of the memory? From this, byte order appears, which is the order in which the low-order bits are stored in memory when data is stored in bytes. This is byte order

Big endian, little endian

Due to the order in which data is stored in memory, there are two orders, and they are two completely opposite orders.

  • 1, Little endian byte order
    Little endian byte order, that is, when data is stored, The low byte of the value is stored at the low address, and the high byte is stored at the high address. As shown below:
    little endian
  • 2, Big endian
    Big endian, that is, when data is stored, The low byte of the value is stored at the high address, and the high byte is stored at the low address. As shown below:
    big endian
P.S. The explanation here begins with the address where the low byte is stored, not the address where the high byte is stored. This is convenient for memory, because low corresponds to small, and high corresponds to large. Moreover, the CPU reads memory starting from the low address.
their respective advantages
  • Little endian:Because the low bit is at the low address, there is no need to adjust the bytes when doing data type forced conversion. , so data type coercion is faster. Since the CPU reads memory data linearly from low address to high address, so reads Fast retrieval
  • Big endian: Becausethe high bit is at the low address, it becomes convenient to judge the sign bit

Here are the reasons for their respective advantages.

  • Little endian: There are two situations when doing data type forced conversion. One is to convert low precision to high precision. This kind of conversion does not matter when stored in big or small endian byte order. Impact, because it is nothing more than filling all the high bits of the converted high-precision with zeros. For example, if the two-byte 0x1234 is to be converted into four bytes, then you only need to fill in the converted high bits with zeros, and it becomes 0x00001234.
    The other is to convert high precision into low precision. This forced data type conversion will inevitably lead to the loss of part of the precision, so you have to consider which part of the precision is discarded. The compiler's conversion principle for this method is to discard the high byte of the value and retain only the low byte data. For example, if the two-byte value 0x1234 is forced to be converted into one byte, the converted value is 0x34.
    Because the low byte is stored at a low address, there is no need to adjust the bytes when doing high-precision forced type conversion to low-precision, because we just As mentioned before, the reading of memory data is linear, that is, the low address is closest to the read position, so when converting this precision , you can directly get the previous low-bit data in one go ( It is for this reason that when the CPU reads memory data, it does not need to shift to a high address, so it saves Without the shift operation, reading will be faster). Then in big-endian byte order, you have to shift to a high address before you can get the lower data

  • Big endian: As we all know, when there is a sign bit, the sign bit is stored in the highest bit of the data. It just so happens that in big-endian byte order, the high bit is stored in the low byte closest to the reading position, so it is relatively convenient to obtain the sign bit. There is no need to shift to the high bit like in little-endian byte order. Address to pick up

Conclusion

It can be seen from here that the big and small endian byte orders are mutually exclusive. If you choose one byte order, you must give up the other byte order. Different CPU architectures choose their own byte order. The typical representative of little endian is our common x86 architecture CPU, and the typical representative of big endian is IBM. as follows:

  • Little endian: x86 (example: Intel and AMD), DEC

  • Big Endian: IBM, PowerPC and Sun

  • ARM system CPUs are even more powerful. They use both big and small endian. However, the specific byte order chosen by ARM must be decided by the hardware manufacturer.

Of course, byte order does not only exist when the CPU accesses memory, but also includes current file storage and network transmission. The network byte order is the selected big-endian byte order, and the storage of bmp format pictures is the selected little-endian byte order. Which one you choose depends on the developer.

Reference book: "Operating System Truth Restore" – written by Zheng Gang

Guess you like

Origin blog.csdn.net/qq_20255275/article/details/119513110