Let's Explore.
Well, it is quite interesting that as human being, computer also write to and read from memory from left-to-right as well as right-to-left.
If you want to address whole title of this blog post in single word, in computer terminology, then the term is endian or endianness.
In a byte-addressable system, where each byte has an index, called its address. A word (four bytes on 32-bit system) are stored in back-to-back memory addresses. However, the question is how!? Which part of data (the most significant byte OR the least significant byte) store at the first memory address!?
We have mainly two options... Big-endian and Little-endian. (I use 'mainly' because, except this two methods, there are bi-endian and middle-endian.)
Big-endian
Let's see how your system laid out given data within memory.
Following Macro will take the first byte of the integer which is casted to unsigned character pointer and check that whether it is 0 or 1. Rest of the code is self-explanatory... I guess!!
In following code, we take an address of integer (int) and cast it to unsigned character pointer (unsigned char *), then we print value of each byte in hex.
Hexadecimal value of 12345 is 0x3039. So, on big-endian system output will be 0x00003039 and in contrast on little-endian system output will be 0x39300000.
Well, it is quite interesting that as human being, computer also write to and read from memory from left-to-right as well as right-to-left.
If you want to address whole title of this blog post in single word, in computer terminology, then the term is endian or endianness.
In a byte-addressable system, where each byte has an index, called its address. A word (four bytes on 32-bit system) are stored in back-to-back memory addresses. However, the question is how!? Which part of data (the most significant byte OR the least significant byte) store at the first memory address!?
We have mainly two options... Big-endian and Little-endian. (I use 'mainly' because, except this two methods, there are bi-endian and middle-endian.)
Big-endian
- In this system, most significant byte of the word is stored in the smallest address given and the least significant byte is stored in the largest. The big-endian format is also known as the Motorola convention.
- Decreasing numeric significance with increasing memory addresses (or increasing time), known as big-endian.
- In this system, least significant byte is stored in the smallest address. Mainly Intel series of processors use the little-endian format, also known as the Intel convention.
- Increasing numeric significance with increasing memory addresses (or increasing time), known as little-endian.
Let's see how your system laid out given data within memory.
I am using Debian Linux on Intel Centrino, which is x86 machine and little-endian by definition.
Following Macro will take the first byte of the integer which is casted to unsigned character pointer and check that whether it is 0 or 1. Rest of the code is self-explanatory... I guess!!
1 2 3 4 5 6 7 | int i = 1; #define endian() { \ ((*(unsigned char *)&i) == 0) \ ? printf("Big-endian") \ : printf("Little-endian"); \ } |
In following code, we take an address of integer (int) and cast it to unsigned character pointer (unsigned char *), then we print value of each byte in hex.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> typedef unsigned char * char_ptr; void show_bytes(char_ptr start, int length); int main(void) { int a = 12345; int *ptr = &a; show_bytes((char_ptr) ptr, sizeof(int)); return 0; } void show_bytes(char_ptr start, int length) { int i; for (i = 0; i < length; i++) { printf("%.2x\n", start[i]); } printf("\n"); } |
Hexadecimal value of 12345 is 0x3039. So, on big-endian system output will be 0x00003039 and in contrast on little-endian system output will be 0x39300000.