Penguin
Annotated edit history of Endianness version 4, including all changes. View license author blame.
Rev Author # Line
1 AristotlePagaltzis 1 ''“There are 10 kinds of people in the world; those who know binary and those who don't.” – Seen on the net.''
2 <br> ''“There are 01 kinds of people who know binary; little-endians and everyone else.” – zcat(1)''
3
4 AristotlePagaltzis 4 The order of bytes in a word. The names “big-endian” and “little-endian” originate from the book “Gulliver’s Travels”, where a tribe of tiny people divide themselves into two factions in a ReligiousWar over which end they should cut their eggs open at – the big end, or the little end. In computer terms, big-endian [CPU]s store the most significant byte at the lowest byte address of a word and progress to less significant bytes at higher addresses, while little-endian machines start with the least significant byte and store progressively more significant ones. A [C] program demonstrates this:
1 AristotlePagaltzis 5
6 <verbatim>
7 #include <stdio.h>
8 int main( void ) {
9 int x = 0xaabbccdd;
10 int i;
11
12 for(
13 i = 0 ;
14 i < sizeof( int ) ;
15 printf( i ? "%x " : "%x", ( (unsigned char*) &x )[ i++ ] )
16 );
17
18 printf( "\n" );
19
20 return 0;
21 }
22 </verbatim>
23
24 On an [x86] system, which is a little-endian architecture, this will print <tt>dd cc bb aa</tt>, showing that the bytes <tt>aa bb cc dd</tt> were stored in “reverse” order. On a [SPARC] it prints the more intuitive <tt>aa bb cc dd</tt>.
25
26 Little-endian was often used in 8-bit machines, since it makes in-memory addition easy for them: load the first byte, increment it, and if it overflows fetch the next byte and increment it. Larger architectures that had no 8-bit roots to begin with didn’t care and were usually big-endian. This explains why nowdays, almost everyone except [x86] uses big-endian order: [SGI]’s [MIPS] systems, [IBM]’s RS6000, POWER and [PowerPC] architectures, SunMicrosystems’ [SPARC], and others are all big-endian systems. The DEC [Alpha] was a notable exception, it used little-endian order.
27
28 Besides the two obvious and common orders, there are also bizzare ones like the PDP’s <tt>02 01 04 03</tt>, and various others, sometimes dubbed “middle endian.” A possible explanation is that there was an attempt at backwards compatibility in the transition from 1-byte to 2-byte words, but not in that from 2-byte to 4-byte words.
29
2 LawrenceDoliveiro 30 Humans using a left-to-right writing system with arabic numbers are big-endian: when we write “1234,” the 1 means one thousand, the 4 means 4, so we write digits in order from the most to the least significant. Humans using right-to-left writing systems with arabic numbers, such as Arabic and Hebrew, are little-endian, because they approach a number such as 1234 from the right, ie from the least significant digit first. Funnily enough, this means that even though the digits have the same geometrical sequence on paper, they are little-endian in one writing system and big-endian in the other.
31
32 Another peculiarity is found in spoken German (or written German with spelled-out numbers): tens and ones are arranged in little-endian order, f.ex. “einundzwanzig” means “one-and-twenty.” However, the more significant digits are arranged in big-endian order: “dreihunderteinundzwanzig” means “three-hundred-and-one-and-twenty”. This is also sometimes seen in older English usage, e.g. “four-and-twenty blackbirds baked in a pie”.
3 LawrenceDoliveiro 33
34 Ultimately, it comes down to reading order versus logical consistency. Big-endian matches the order in which most of us read things from left-to-right, while little-endian simplifies the relationship between three different numberings:
35 * that of the binary digits of an integer—call this ''i''
36 * that of bits within a byte—call this ''b''
37 * that of bytes within a word—call this ''B''
38
39 For instance, the bits in a byte are numbered 0-7. A byte can hold an unsigned integer in the range 0 .. 255. Does the bit numbered 0 represent the 2**0 digit, bit 1 represent 2**1, etc? It might or might not—this is a convention defined by the CPU architecture. And what happens with, say, a two-byte integer? Does byte 0 hold bits 0-7 and byte 1 hold bits 8-15, or vice versa? This is where the endianness of the CPU architecture comes in.
40
41 Suppose the numbering of a bit in an ''N''-byte integer is ''j''. In little-endian architectures, the following are always true:
42 * ''b'' = ''j'' __mod__ 8
43 * ''B'' = ''j'' __div__ 8
44 * ''i'' = ''j''
45
46 In big-endian architectures, the situation is more complicated. For example, in the Motorola 680''x''0 architecture, the first and third of the above equations still hold, while the second one becomes
47 * ''B'' = (8 * ''N'' - 1 - ''j'') __div__ 8
48
49 while with the IBM PowerPC, all three equations are different:
50 * ''b'' = 7 - ''j'' __mod__ 8
51 * ''B'' = (8 * ''N'' - 1 - ''j'') __div__ 8
52 * ''i'' = 8 * ''N'' - 1 - ''j''
53
54 As you can see, the simplest form of relationships is in the little-endian case.
1 AristotlePagaltzis 55
56 See also:
57 * NetworkByteOrder