Saturday, August 1, 2009

Check whether computer is Little Endian / Big Endian ?

Current post is on how to check whether computer is Big Endian or Little Endian.


It is a common technical interview question to test your knowledge on computer architecture.

Endianness refers - the order in which computer stores the bytes.

Little Endian - Stores LSB (Least Significant Byte) in lowest address.

Big Endian - Stores MSB (Most Significant Byte) in highest address.

Answer:

/* Function returns whether the computer system is Little or Big Endian*/

bool IsLittleEndian()
{
       int TestNumber = 1;
       char * ptr;

       ptr = (char *) &TestNumber;

       return (*ptr);    //True if little Endian.
}

Appreciate your feedback via comments. Thanks.

No comments:

Post a Comment

Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.