Little Endian vs. Big Endian
- You all know how to convert a
decimal
number to binary.
For example, 6510 = 010000012.
- 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
|
- The first approach, "Big
Endian", stores the
most significant byte first.
In other words, the most significant byte is stored at the lowest
address.
- The second approach, "Little Endian", stores the
least significant byte first.
In other words, the most significant byte is stored at the highest
address.
- 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.
- Intel 8086 and its
x86
successors use little-endian.
Therefore, I bet most computer platforms you encounter now are
little-endian.
- Let's use pointers to write a C program to inspect whether
lilina.csie.ncnu.edu.tw is little-endian or big-endian.
- Hint: Python has a system constant to indicate the endianness.
Run "
python3 -c 'import sys; print(sys.byteorder)'
".
- First, develop a function
dump_byte()
to print the bit sequence in a
byte. For example,
- dump_byte(65) will print "01000001".
- dump_byte(255) will print "11111111".
- 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.
- 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.
- You may test your program with the following values:
- 4 will be shown as 00000100 00000000.
- 256 will be shown as 00000000 00000001.
- These results confirm that our program is running on a
little-endian machine.
- Predict the result when n = 0xFEDC.
11011100 11111110
- If n is a 4-byte int (say 0x01020304), predict the result.
00000100 00000011 00000010 00000001