Big Endian Little Endian

How will you find if a machine is a big endian or little endian?

Questions by rdreddy

Showing Answers 1 - 9 of 9 Answers

deepak4bin

  • Aug 13th, 2010
 

You can write a small C program to find if a system is big Endian or little Endian
void main()

{

short
i = 1;
char
*ptr = (char*)&i;

if
(*ptr == 0)
printf("big endian");
else

printf("little endian");

}

Variable i will have value 0x0001 in two bytes. One byte will contain 0x00 and other byte will contain 0x01. If the byte in memory with lower address (pointed by ptr) will contain 0x00 (which is higher order byte of i), system is big endian.


poojarinke

  • Aug 13th, 2010
 

Some machines store, for example, a two-byte integer with the least significant byte first, followed by the most significant byte. These machines are called little endian machines.
Other machines store a two-byte integer with its most significant byte first, followed by it least significant byte. These machines are called big endian machines.
Most UNIX machines are big endian. Whereas most PCs are little endian machines.

For example, suppose the value 10405 is written as a two byte integer on a little endian machine. The binary representation of this number is this, with the most significant byte shown on the right:

   0 0 1 0 1 0 0 0   1 0 1 0 0 1 0 1

If you read this value on a big endian machine, the number will be represented with the bytes swapped, or as this:

   1 0 1 0 0 1 0 1  0 0 1 0 1 0 0 0   

mohamr2

  • Sep 20th, 2010
 

The usual techniques are to use a pointer:
int x = 1;
if(*(char *)&x == 1)
printf("little-endiann");
else
printf("big-endiann");

or a union:
union {
int i;
char c[sizeof(int)];
}
x;
x.i = 1;
if(x.c[0] == 1)
printf("little-endiann");
else
printf("big-endiann");
 


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions