Sunday, September 28, 2014

Determining Endianness of Native Processor


We generally encounter a very common term 'Endianness' while working in embedded programming.

Basically endianness is the way a machine architecture stores the data in the memory.We have two types of endianness - Little Endian and Big Endian.

In Little Endian architecture, lower byte data (LSB) is stored in lower address.
In Big Endian architecture, higher byte data (MSB) is stored in lower address.

Though various optimized and non optimized methods are available on internet, I have one more way of determining endianness of native processor.I guess it is one more non optimized method but yeah it is one method.

C code for determining endianness:

//This program determines the Endianness of Native Processor
#include <stdio.h>
void main()
{
int val1 = 256;
char val2 = 0;
val2 = (char*)val1;
if(0 == val2)
    printf("Native processor in Little Endian\n");
else
    printf("Native processor in Big Endian\n");
}

Please ignore warnings during compilation.GNU will give warnings because of non compatible typecasting.

No comments:

Post a Comment