網頁

2011年1月9日 星期日

Little Endian & Big Endian


Little Endian and Big Endian is an important concept in Embedded System and Firmware field. People who don't understand it will not be hired; people who clearly understands it and answer this kind of questions well can impress the interviewers.

Different machines (CPU + OS) might use different Endianness. For example, Linux and Windows on x86 is Little Endian, but Linux on MIPS is Big Endian.

In the following article, we use the integer 0x12345678 to illustrate the definitions of Little/Big Endian.

Little Endian
"Little Endian" means that the Low Significant Byte of a data is stored in memory at the lowest address, and the High Significant Byte at the highest address.
mem addr
data
0x4000
0x78
0x4001
0x56
0x4002
0x34
0x4003
0x12
Big Endian
"Big Endian" means that the High Significant Byte of a data is stored in memory at the lowest address, and the Low Significant Byte at the highest address.
mem addr
data
0x4000
0x12
0x4001
0x34
0x4002
0x56
0x4003
0x78
The advantage of Big Endian is that you can always determine whether a number is positive of negative by looking at the byte at offset zero, you don't need to know how long the number is, nor to skip over any bytes to find the byte containing the sign information. Also, when you print a number, you can print it from lowest bytes to highest bytes, and that is more easy and efficient.

Write a program to check the Endianness (Little or Small) on your machine
union {   // please make sure you understand "union"
    unsigned int i;    
    unsigned char c;
} u;
int main()
{
    u.i = 1;
    if(u.c == 1)
        printf("little edian\n");
    else 
        printf("big endian\n");
}

1 則留言:

  1. Good information. Please, visit my blog as well. www.embeddeddesignblog.blogspot.com

    回覆刪除