Little Endian or Big Endian?

Question: Is your machine little or big endian?
Answer: Let first take a look at the solution before we discuss about it.
int isLittleEndian( void )
{
    unsigned int temp = 0x12345678;
    char * tempAddress = &temp;
    if( *tempAddress == 0x12 )
    {
        return 0; // Indicating False
    } else {
        return 1; // Indicating True
    }
}

To answer this, you have to obviously know what little and big endian are. Also you need some basic understanding of variable assignments for data types of varying sizes. A major mistake which you could make is with the temp variable. When I was asked this question, I answered it by taking a fixed address for temp which is insanely dangerous (I know I was dumb). The address could have been used for something else or even been a reserved address for all you know. By creating a local variable, temp will be placed on the stack so we don’t have to worry about whether it will be safe or not.

Comments

Popular posts from this blog

Boxes of Money

Pick a Random Byte

Singly Linked List – Delete Node