How to decide whether given processor is using little endian format or big endian format ?

It can be found easily with a small c/c++ program. Before going to this first we shall understand what is the endian. Endian is of two types i.e., little endian and big endian. Endian means how a processor stores a variable in it's memory say left to right or right to left.

Big endian means that the most significant byte of any multibyte data field is stored at the lowest memory address, which is also the address of the larger field.

Little endian means that the least significant byte of any multibyte data field is stored at the lowest memory address, which is also the address of the larger field

Where as Big endian means it stores in right to left. Rarely used in some microprpcessors.

Program to find a little endian or big endian

void print_Endian()
{
   unsigned int i = 1;
   char *c = (char *) &i;
   if(*c)
   {
       printf("\n\rLittle Endian");
   }
   else
   {
      printf("\n\rBig Endian");
   }
}

Explanation:

Consider this architecture uses 4 bytes for int. So int i is declared as 1. it would be stored in a little endian architecture as shown below


If it is a big endian then it will be exactly opposite for the picture shown above.

char *c = (char *)&i this line declares a char pointer that points to the address of i. As it is a char pointer it's value will be 1st 1 byte of the i. So that value of *c will be 1 but it is 1 byte as shown below



If it is a big endian the value on char* will be 0.

So that the if statement will print the type of endian for the processor.

Video Tutorial


Also See Program to swap little to bit endian and viceversa of the same