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
Big-endian memory layout is most significant bytes first, whereas little-endian layout is least significant bytes first. Given the value 0xA15D23B1:
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:
A good way to remember "which is which":
For example, when regarding the word
0xA15D23B1
as a sequence of bytes, a big-endian machine starts it from the most significant byte0xA1
. 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.
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:
For a 4-byte quantity, there are more options, but there are still two primary ones (plus a historical curiosity):
Note that a number of modern chip sets are bi-endian — can be switched to run in big-endian or little-endian mode:
No. In little-endian it's 23