Endian | First Byte (lowest address) | Middle Bytes | Last Byte (highest address) | Summary |
---|---|---|---|---|
big | most significant | ... | least significant | Similar to a number written on paper (in Arabic numerals) |
little | least significant | ... | most significant | Arithmetic calculation order (see carry propagation) |
increasing addresses → | |||||
... | 0Ah | 0Bh | 0Ch | 0Dh | ... |
The most significant byte (MSB) value, which is 0Ah in our example, is stored at the memory location with the lowest address, the next byte value in significance, 0Bh, is stored at the following memory location and so on. This is akin to Left-to-Right reading in hexadecimal order.
Atomic element size 16-bitincreasing addresses → | |||
... | 0A0Bh | 0C0Dh | ... |
The most significant atomic element stores now the value 0A0Bh, followed by 0C0Dh.
Little-endianAtomic element size 8-bit, address increment 1-byte (octet)increasing addresses → | |||||
... | 0Dh | 0Ch | 0Bh | 0Ah | ... |
The least significant byte (LSB) value, 0Dh, is at the lowest address. The other bytes follow in increasing order of significance.
Atomic element size 16-bitincreasing addresses → | |||
... | 0C0Dh | 0A0Bh | ... |
The least significant 16-bit unit stores the value 0C0Dh, immediately followed by 0A0Bh. Note that 0C0Dh and 0A0Bh represent integers, not bit layouts (see bit numbering).
← increasing addresses | |||||
... | 0Ah | 0Bh | 0Ch | 0Dh | ... |
The least significant byte (LSB) value, 0Dh, is at the lowest address. The other bytes follow in increasing order of significance.(这个明显符合我们的习惯)
With 16-bit atomic elements:
← increasing addresses | |||
... | 0A0Bh | 0C0Dh | ... |
The least significant 16-bit unit stores the value 0C0Dh, immediately followed by 0A0Bh.
The display of text is reversed from the normal display of languages such as English that read from left to right. For example, the word "XRAY" displayed in this manner, with each character stored in an 8-bit atomic element:
← increasing addresses | |||||
... | "Y" | "A" | "R" | "X" | ... |
If pairs of characters are stored in 16-bit atomic elements (using 8 bits per character), it could look even stranger:
← increasing addresses | |||
... | "AY" | "XR" | ... |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- char a[] = {'a', 'b', 'c'};
- char b[] = {'d', 'e', 'f'};
- a[3] = 0;
- printf("strlen(a)=%d, strlen(b)=%d\n", strlen(a), strlen(b));
- printf("a=%s, b=%s\n", a, b);
- printf("sizeof(a)=%d, sizeof(b)=%d\n", sizeof(a), sizeof(b));
- return 0;
- }
a=abc, b=defabc
sizeof(a)=3, sizeof(b)=3
- #include <stdio.h>
- int main()
- {
- unsigned long array[] = {0x12345678, 0xabcdef01, 0x456789ab};
- unsigned short ret;
- ret = *((unsigned short *)((unsigned long)array+7));
- printf("0x%x\n", ret);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main(void){
- int a[5]={1,2,3,4,5};
- int *ptr =(int *)(&a+1);
- printf("%d,%d\n",*(a+1),*(ptr-1))
- return 0;
-
- }
- #include <stdio.h>
- #include <assert.h>
- int main()
- {
- unsigned short x = 0xff01;
- assert(sizeof(x) >= 2);
- if(*(char*)&x == 1) //if(char(x) == 1)
- printf("little-endian\n");
- else if((char)x > 1)
- printf("big-endian\n");
- else
- printf("unknown\n");
- return 0;
- }
- #include <stdio.h>
- int main()
- {
- union{
- char c;
- int i;
- }u;
- u.i = 0x0201;
- if(u.c == 1)
- printf("little-endian\n");
- else if(u.c == 2)
- printf("big-endian\n");
- else
- printf("unknown\n");
-
- return 0;
- }
- #include <stdio.h>
- union u{
- struct {
- char i:1;
- char j:2;
- char m:3;
- } s;
- char c;
- }r;
- int main()
- {
- r.s.i = 1; // 1
- r.s.j = 2; // 10
- r.s.m = 3; // 011
- printf("0x%x\n", r.c);
- return 0;
- }
- #include <stdio.h>
- union {
- struct
- {
- unsigned char a1:2;
- unsigned char a2:3;
- unsigned char a3:3;
- }x;
- unsigned char b;
- }d;
- int main(int argc, char* argv[])
- {
- d.b = 100; //100 == 0110 0100
- printf("0x%x\n0x%x\n0x%x\n", d.x.a1, d.x.a2, d.x.a3);
- return 0;
- }