问题
Suppose I have unsigned long long x = 0x0123456789ABCDEF
.
Which of the following is correct? (I can verify only the first one):
- On a 32-bit little-endian processor, it will appear in memory as
67 45 23 01 EF CD AB 89
. - On a 64-bit little-endian processor, it will appear in memory as
EF CD AB 89 67 45 23 01
. - On a 32-bit big-endian processor, it will appear in memory as
01 23 45 67 89 AB CD EF
. - On a 64-bit big-endian processor, it will appear in memory as
01 23 45 67 89 AB CD EF
.
回答1:
The first one is wrong. On ia32 at least the layout is EF CD AB 89 67 45 23 01
.
The others are correct.
回答2:
Little endian means the least-significant bits are in the first byte, and big endian means the least-significant bits are in the last byte:
0x0123456789ABCDEF big endian is 0x01, 0x23, 0x45 ...
0x0123456789ABCDEF little endian is 0xEF, 0xCD, 0xAB ...
The native word endianess and size of the processor is inconsequential; the appearance in memory is dictated by the endian.
回答3:
I'd say the 32-bit solution is very much up to the compiler. It can choose to represent this type that it lacks native support for in any way it pleases, as long as the size is the expected one.
The 64-bit ones I'd agree with as being correct.
来源:https://stackoverflow.com/questions/21478765/endian-representation-of-64-bit-values