What does Little Endian & Big Endian?

xiaoxiao2021-03-05  23

What does Endian in this title? Let us first take a look at the situation below, this is the content in a Word value in memory, then the value in this word is 0x1234, or is the 0x3412?

Low Byte High Byte

0x12 0x34

Those familiar with X86 assembly immediately knew that this value should be 0x3412, it is right, but in some cases, if you see this situation on SGI machine, it is just the opposite, 0x1234 is the correct answer, this with CPU The internal processing data is related. Both processing methods exist in the CPU produced by different vendors. If this Word value is 0x3412, we call Little-Endian, if we are 0x1234, we call Big-endian, this It is two different Byte ORDERS. More precise definitions in MSDN are as follows:

Byte Order by ORDERING Meaning

BIG-Endian The Most Significant byte Is on The Left End of a Word.

Little-Endian The Most Significant Byte Is on The Right End of a Word.

Generally speaking, we don't have to care about byte Ordering, but to consider this issue if there is a communication and resource sharing between cross-platform. Maybe you will say, I will never use other non-x86 CPUs, maybe this, you can even don't know what we are most commonly used Intel, AMD, etc. Is Little-Endian, and press now Looking at the number of installed, it can be said that the vast majority of the CPU in the world is Little-endian, but know more about there is no harm, perhaps a useful day, actually if you want to talk to network programming, understand some or help, After reading this article, you should know why Socket programming is like NTOHL, HTONL, NTOHS, and HTons seem to be strange, and it is easy to understand the meaning of these functions.

Suppose we have to transfer and exchange data between different Byte ORDERING machines, what should I do? There are two ways, one is all converted into text to transmit (such as XML use), and another way is in accordance with some part BYTE ORDER, the problem of mutual conversion between different Byte ORDERs (network transmission standards, such as TCP / IP, using the second method and due to historical reasons, byte ordering is BIG-Endian). How to convert between two types? There are a lot of ways, we can first look at the method (list) used in the MFC in handling the Serialize code, although the code should be efficient, but I don't like it, the reason is that I think this is not a common Beautiful method. The following is the code that I wrote in my own:

Template

F3D_INLINE T ConvertIndian (t t)

{

T TRESULT = 0;

For (int i = 0; i

{

TResult << = 8;

TRESULT | = (T & 0xFF);

T >> = 8;

}

Return TRESULT;

}

The principle is very simple, exchange byte order, I don't have much to say, of course, this way is not fast, but general (I don't have conditional test, if there is something wrong, please point out), if you want to quickly code, you can Different Platform use the code related to Platform, such as "Load Word Byte-Reverse Indexed" (LWBRX) instruction on PowerPC, you can also use BSWAP single assembly instructions on x86. The general code specified for INT16, int32 is much more than this. Of course, if you use this conversion function if you use this conversion function, we can define a macro to handle different BYTE ORDERING, you can also test byte ordering at runtime, the following code gives a simple test method.

// Test for endianness.

F3D_INline Bool Islittlendian (Void)

{

DWORD dwTestValue = 0x12345678L;

Return (* (BYTE *) & dwtestValue) == 0x78);

}

But Float is more monster, it is possible to involve problems that are not just byte Order, because some platforms have to be converted by Alpha does not use IEEE floating point format. Of course, the other method one is to input the Float used in text, and another means to convert it to a fixed point to re-process in some cases. Here I no longer go deep.

If you read and write a third party, you have specified Byte Order's file or data stream, such as reading SGI bitmap file format, you can directly press the specified Byte Order, you don't have to consider what of the Host is byte ordering. Let's give the corresponding code:

// read a little-endian type from address

Template

F3D_INLINE T GETLITTLEENDIAN (Const Byte * PBUF)

{

T TRESULT = 0;

PBUF = SizeOf (T) - 1;

For (int i = 0; i

{

TResult << = 8;

TRESULT | = * PBUF -;

}

Return TRESULT;

}

// read a big-endian type from address

Template

F3D_INLINE T getBiGendian (const byte * pbuf)

{

T TRESULT = 0;

For (int i = 0; i

{

TResult << = 8;

TRESULT | = * PBUF ;

}

Return TRESULT;

}

// set a little-endian type on a address

Template

F3d_inline void setlittlendian (byte * pbuf, t t)

{

For (int i = 0; i

{

* PBUF = Byte (T & 0xFF);

T >> = 8;

}

}

// set a big-endian t on a address

Template

F3D_INLINE VOID SetBiGendian (byte * Pbuf, T T) {

PBUF = SizeOf (T) - 1;

For (int i = 0; i

{

* PBUF - = Byte (T & 0xFF);

T >> = 8;

}

}

As can be seen from above, Byte Order is quite simple. In general applications, it may not be used, but if you are interested in writing cross-platform, it must be understood. The above code is taken from the actual source code.

Attachment: Common Processor, ORDERING situation

Processor OS Order

X86 (Intel, AMD, ...) All Little-Endian

Dec Alpha All Little-Endian

HP-PA NT Little-Endian

HP-PA UNIX BIG-ENDIAN

Sun sparc all? Big-endian

MIPS NT LITTLE-ENDIAN

MIPS UNIX BIG-ENDIAN

PowerPC NT Little-Endian

PowerPC NON-NT BIG-ENDIAN

RS / 6000 Unix Big-Endian

Motorola M68k All Big-Endian

转载请注明原文地址:https://www.9cbs.com/read-32265.html

New Post(0)