Endianness
I found a paper about endianness while I was looking on Intel's developer documentations. I have been wondering what kind of advantages can be gained to store the data in little endian or big endian format. There are some explained advantages and disadvantages in that document. However, I haven't been enough convinced with these explanations. I think there are many others related to CPU and hardware design.
Anyway, that document well describes the endianness and the portability issues. It should be read if you have got some questions in mind.
I implemented a function which checks the endiannes of the CPU on run time. It is possible to decide the architecture by comparing one byte. However, I like to perform strict check on multi-byte data.
Here is the link of the Intel's document.
union {
char array[sizeof(int)];
int intVal;
} testUnion;
int main()
{
int i, bigEndVal = 0, littleEndVal = 0;
char c = 'a';
for(i = 0; i < sizeof(int); i++) {
testUnion.array[i] = c;
bigEndVal = (bigEndVal << 8) | c;
littleEndVal = littleEndVal | (c << 8 * i);
c++;
}
if (testUnion.intVal == bigEndVal)
printf("Big Endiann");
else if (testUnion.intVal == littleEndVal)
printf("Little Endiann");
else
printf("Unknownn");
return 0;
}