Reputation: 9231
I was wondering, why some architectures use little-endian and others big-endian. I remember I read somewhere that it has to do with performance, however, I don't understand how can endianness influence it. Also I know that:
The little-endian system has the property that the same value can be read from memory at different lengths without using different addresses.
Which seems a nice feature, but, even so, many systems use big-endian, which probably means big-endian has some advantages too (if so, which?).
I'm sure there's more to it, most probably digging down to the hardware level. Would love to know the details.
Upvotes: 60
Views: 22904
Reputation: 5322
Although the previous two answers do a great job explaining the “what”s behind endianness, “why” is left unexplained.
The real “why” behind endian lies in how the English language writes numbers and words in opposite orders:
Still not convinced English writes forwards and backwards? Consider the following graphic to visualize it:
Graphics in English conventionally go the same direction as text—left to right. This means it "feels" normal to an English brain to see the indices above increasing 0 1 2 3 4 5 6 7 8 9
left-to-right across the graphic because the brain is naturally reading the graphic the same way it reads text.
OK, now that we’ve established the graphic above is correctly oriented and not backwards, observe how concatenating the memory contents (0x11 0x22 0x33 0x44 0x55 0x66 0x77
) left-to-right like text yields the big endian number 1122334455667788
, not little endian.
The only way to naturally get the little endian number out of the graphic would be if English wrote text right to left, making the graphic’s zero index be on the left and the indices go 7 6 5 4 3 2 1 0
:
I hope the conflict between text writing direction and numeric writing direction is evident now.
Why does English matter at all when programming is a universal activity done around the world? It’s because all the early standards and tech innovations got their first footing in the United States and Great Britain. English is why the predominant Morse Code variant used for telegraphs only had A-Z and 0-9. English is why 7-bit ASCII doesn’t have accented letters.
This StackOverflow answer passes no judgement on whether the United States and Great Britain and their promulgation of English is good or bad because that’s beside the point. The point is that history has been written, history cannot be changed, and the history on record is English heavily influencing many technologies and standards due to the United States and Great Britian.
If, hypothetically, the English language wrote numbers the same direction as its words, computer standards would have evolved with Big Endian as the universal obvious choice. The performance differences between big endian and little endian can, sometimes, impact software efficiency, but has been completely irrelevant to hardware for at least 40 years.
If a computer’s instruction set deals with integers as little endian, nothing says the underlying hardware has to fetch memory in little endian and, historically, there’s a lack of any correlation between software endianness and hardware endianness. For example, various early big endian computers stored physical memory little endian and there’s cases of vice-versa because it makes no difference to the software how the hardware works internally. In very early computers, oftentimes, the first model of a processor would match the software in endianness, then later models could be anyone’s guess as they preserve the software endianness for backwards compatibility and whatever hardware endianness make the most sense for each model.
In reality, due to English writing numbers and text backwards, we have two camps of people. Those who find it easiest to imagine the memory address space as a sequence of numbers, e.g. 4-byte ints, find Little Endian the most intuitive; those who foremost imagine computer memory as one long string of ASCII bytes usually find Big Endian the most intuitive.
Electrical engineers designing the circuits often fall into the latter camp with Big Endian, which is why so many early computer systems were BE. Software engineers writing C deal almost exclusively with automatic (stack and globals) and indexed (array) addressing, which makes Little Endian often more appealing.
Upvotes: -3
Reputation: 25204
I've looked around the net a bit for more information on this question and there is a quite a range of answers and reasonings to explain why big or little endian ordering may be preferable. I'll do my best to explain here what I found:
The obvious advantage to little-endianness is what you mentioned already in your question... the fact that a given number can be read as a number of a varying number of bits from the same memory address. As the Wikipedia article on the topic states:
Although this little-endian property is rarely used directly by high-level programmers, it is often employed by code optimizers as well as by assembly language programmers.
Because of this, mathematical functions involving multiple precisions are easier to write because the byte significance will always correspond to the memory address, whereas with big-endian numbers this is not the case. This seems to be the argument for little-endianness that is quoted over and over again... because of its prevalence I would have to assume that the benefits of this ordering are relatively significant.
Another interesting explanation that I found concerns addition and subtraction. When adding or subtracting multi-byte numbers, the least significant byte must be fetched first to see if there is a carryover to more significant bytes. Because the least-significant byte is read first in little-endian numbers, the system can parallelize and begin calculation on this byte while fetching the following byte(s).
Going back to the Wikipedia article, the stated advantage of big-endian numbers is that the size of the number can be more easily estimated because the most significant digit comes first. Related to this fact is that it is simple to tell whether a number is positive or negative by simply examining the bit at offset 0 in the lowest order byte.
What is also stated when discussing the benefits of big-endianness is that the binary digits are ordered as most people order base-10 digits. This is advantageous performance-wise when converting from binary to decimal.
While all these arguments are interesting (at least I think so), their applicability to modern processors is another matter. In particular, the addition/subtraction argument was most valid on 8 bit systems...
For my money, little-endianness seems to make the most sense and is by far the most common when looking at all the devices which use it. I think that the reason why big-endianness is still used, is more for reasons of legacy than performance. Perhaps at one time the designers of a given architecture decided that big-endianness was preferable to little-endianness, and as the architecture evolved over the years the endianness stayed the same.
The parallel I draw here is with JPEG (which is big-endian). JPEG is big-endian format, despite the fact that virtually all the machines that consume it are little-endian. While one can ask what are the benefits to JPEG being big-endian, I would venture out and say that for all intents and purposes the performance arguments mentioned above don't make a shred of difference. The fact is that JPEG was designed that way, and so long as it remains in use, that way it shall stay.
Upvotes: 59
Reputation: 12610
I would assume that it once were the hardware designers of the first processors who decided which endianness would best integrate with their preferred/existing/planned micro-architecture for the chips they were developing from scratch.
Once established, and for compatibility reasons, the endianness was more or less carried on to later generations of hardware; which would support the 'legacy' argument for why still both kinds exist today.
Upvotes: 2