Little-endian and Big-endian

2020-05-07 06:10发布

I must write a routine for conversion between the 2 representations. But I'm a bit confused. If I have an architecture with a memory with words of 32 bits and I must store the word 0xA15D23B1 with Big-endian the memory become A1 after 5D after 23 and in the end B1 with Little-endian rather the memory is B1 after 23 after 5D and after A1 Is it right?

If I can address the individual bytes on my machine and the address of my word is zero who is the byte 1? In Big-endian is 5D? and in little-endian is 5D too?

Thanks to everyone who will answer me

标签: c endianness
4条回答
放我归山
2楼-- · 2020-05-07 06:45

Big-endian memory layout is most significant bytes first, whereas little-endian layout is least significant bytes first. Given the value 0xA15D23B1:

Memory address    0  1  2  3
Big-endian       A1 5D 23 B1
Little-endian    B1 23 5D A1

Note that big-endian memory layout does not change with respect to word size, but little-endian does. If you consider two short words (16 bit), 0xA15D and 0x23B1 stored contiguously:

Memory address    0  1  2  3
Big-endian       A1 5D 23 B1
Little-endian    5D A1 B1 23
查看更多
手持菜刀,她持情操
3楼-- · 2020-05-07 06:53

A good way to remember "which is which":

Big-endian starts from the big (most-significant) end; little endian starts from the little end.

For example, when regarding the word 0xA15D23B1 as a sequence of bytes, a big-endian machine starts it from the most significant byte 0xA1. It will be stored at the lowest address (this is the meaning of the potentially confusing word "start").

By the way, if you only want to convert from big-endian to little-endian or back, you don't have to understand this: just reverse the order of bytes! This is why many people don't bother to understand what "big-endian" or "little-endian" means - you generally only need to understand whether or not to swap bytes.

查看更多
再贱就再见
4楼-- · 2020-05-07 06:57

There are various ways that processors implement big-endian and little-endian — for a detailed discussion, consult the Wikipedia article on Endianness.

For a 2-byte quantity, there are just two options:

Value:           0x1234 (MSB = 0x12, LSB = 0x34)
Little-endian:   LSB then MSB    0x34  0x12  — Intel, …
Big-endian:      MSB then LSB    0x12  0x34  — SPARC, PowerPC, …

For a 4-byte quantity, there are more options, but there are still two primary ones (plus a historical curiosity):

Value:           0x12345678 (MSB = 0x12, NMSB = 0x34, NLSB = 0x56, LSB = 0x78)
Little-endian:   LSB, NLSB, NMSB, MSB    0x78  0x56  0x34  0x12
Big-endian:      MSB, NMSB, NLSB, LSB    0x12  0x34  0x56  0x78
PDP-11:          NMSB, MSB, NLSB, LSB    0x34  0x12  0x78  0x56

Note that a number of modern chip sets are bi-endian — can be switched to run in big-endian or little-endian mode:

Some architectures (including ARM versions 3 and above, PowerPC, Alpha, SPARC V9, MIPS, PA-RISC, SuperH SH-4 and IA-64) feature a setting which allows for switchable endianness in data segments, code segments or both.

查看更多
Emotional °昔
5楼-- · 2020-05-07 06:58
  • and in little-endian is 5D too?

No. In little-endian it's 23

查看更多
登录 后发表回答