Little Endian vs. Big Endian

  1. You all know how to convert a decimal number to binary. For example, 6510 = 010000012.
  2. However, have you wondered how a 4-byte integer is stored in memory? Suppose the integer 0x12345678 is stored at 0x00400000~0x00400003, do you think it is stored as
    Address Value
    0x00400000 12
    0x00400001 34
    0x00400002 56
    0x00400003 78
    or
    Address Value
    0x00400000 78
    0x00400001 56
    0x00400002 34
    0x00400003 12
  3. The first approach, "Big Endian", stores the most significant byte first. In other words, the most significant byte is stored at the lowest address.
  4. The second approach, "Little Endian", stores the least significant byte first. In other words, the most significant byte is stored at the highest address.
  5. These two approaches are adopted by different CPU families. The IBM System/360 uses big-endian byte order. Motorola 68000 series of processors used the big-endian format. Sun SPARC historically used big-endian. IBM POWER processors were big-endian, too.
  6. Intel 8086 and its x86 successors use little-endian. Therefore, I bet most computer platforms you encounter now are little-endian.
  7. Let's use pointers to write a C program to inspect whether lilina.csie.ncnu.edu.tw is little-endian or big-endian.
  8. First, develop a function dump_byte() to print the bit sequence in a byte. For example,
  9. Then, in your main program, suppose you have a short integer n (2 bytes), you can have a pointer of unsigned char p, which points to the first byte of n.
  10. You may retrieve the first byte by *p, pass the byte to dump_byte() to print its bit sequence. Then retrieve the second byte by *(p+1), and print its bit sequence.
  11. You may test your program with the following values:
    1. 4 will be shown as 00000100 00000000.
    2. 256 will be shown as 00000000 00000001.
    3. These results confirm that our program is running on a little-endian machine.
  12. Predict the result when n = 0xFEDC. 11011100 11111110
  13. If n is a 4-byte int (say 0x01020304), predict the result. 00000100 00000011 00000010 00000001