0
votes

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

4
No, it's not right, you've got it backwards. In little endian, the least significant byte has the lowest address.The Paramagnetic Croissant
Your first paragraph is correct, except for the incorrect use of the word "after" which makes it a bit confusing (I think you meant "followed by" instead of "after"). I don't understand what you're asking in the second paragraph.interjay
You already answered your question. "B1 after 23 after 5D and after A1." Can you describe your actual programming problem? Address zero is normally unavailable to programs. And you can use functions like hton to avoid having to deal with endian issues entirely.Raymond Chen
(Also, what does this have to do with generic lists?)Raymond Chen
What is your programming need? Do you want convert from little endian to big endian or vice versa?Chintan Patel

4 Answers

6
votes

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
2
votes

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.

1
votes

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.

0
votes
  • and in little-endian is 5D too?

No. In little-endian it's 23