LET'S TALK TECHNICAL

This blog is intended to help people prepare for the job interviews and improve their analytical skills. We have posted difficult datastructures and algorithm questions and puzzles. Interview experiences section is for the people to post their interview experiences.Views expressed here are of their personal and the blog author doesn't take any responsibility for the same.

-

Followers

Jobs

Friday, November 9, 2007

How can I test whether my system’s byte order is big endian or little endian.?

Little endian and Big Endian refer to the layout of byte in which the data is stored.

Big endian : Stores most significant data of information from the base address.
Little Endian : Stores from least significant data from the base address.

let us take an example :

I want to store 14 in an integer on a 32bit machine.

in the binary pattern the number is represented as 1110.
If the same is to be stored in a 32bit alignment.

Big Endian stored it like :

0000-0000 0000-0000 0000-0000 0000-1110

Little Endian stores it like :
0000-1110 0000-0000 0000-0000 0000-0000

So the following program might help you in knowing whether your machine is of Little-Endian or Big Endian Architecture.

void main()
{
int i = 1;
char *c = (char *)&i;

if ( *c == 1 )
{
cout<<" LITTLE ENDIAN";
}
else
{
cout<<" BIG ENDIAN";
}
}

No comments:

Post a Comment

Popular Posts