In a previous post “Little and Big Endian conversion” i have briefly discussed about the Big-Endian and the Little-Endian representations. It is the ordering of the bytes (elementary addressable elements) within the representation of a larger basic data type. These are the two byte orderings in the memory within a larger datatype. For example if the size of integer is 4 bytes in a system, then will the least significant byte be stored in the lower memory address or in the higher memory address. In short: if the most significant byte is stored in higher memory address then it is called the Little Endian representation, and if the most significant byte is stored in lower memory address than the least significant byte then it is called the Big-Endian representation. For a diagram see the post “Little and Big Endian conversion”. Also refer the Wikipedia Entry on Endianness.

So how to determine if your system a Little Endian system or a Big Endian system by running a piece of code? It can be done by storing a value in a larger elementary data element in the memory then access the stored value as an array of smallest addressable element. Specifically, we can store an integer in the memory and then access the integer byte by byte, like an array. Here the larger elementary datatype is the integer and the smallest addressable element is a byte. For example if we store the integer value 1 in an unsigned integer variable, the in memory representation will be all bits zero and the 0th bit 1. The hexadecimal representation will be 0x00000001. Then in case of Little Endian system the least significant byte 01 will be found in the first (lower) memory location when accessing the integer byte by byte, and if the system is Big Endian system then the first (lower) memory location will contain 00, which is the most significant byte in the integer (the 01 byte will be stored in the 4th memory location). The below diagram tells the story.

Stored value: 0x00000001

In memory representation:

       Memory Address

  Low                   High
   0       1      2      3
+------+------+------+------+
|  00  |  00  |  00  |  01  |      Big Endian Representation
+------+------+------+------+      stores the least significant byte
                                   in higher memory address. So we get
                                   0 in lower address.
  Low                   High
   0       1      2      3
+------+------+------+------+
|  01  | 00   |  00  |  00  |      Little Endian Representation
+------+------+------+------+      stores the least significant byte
                                   in lower memory address.

As another example if x = 0xabcd1234, then in the case of Little Endian representation will get 0x34 at the first byte, and in the case of Big Endian representation we will get 0xab as the contents of the first byte.

Therefore now the thing is how to access the integer byte by byte like an array. Here is how we do it.

unsigned int x = 1;
char *x_arr = (char *)&x;

Above we assign x = 1 and then assign the address of x to a char * type variable x_arr. Therefore now through x_arr we can access each byte starting from address of x, which will allow us accessing the integer x byte by byte (pointer arithmetic: if we did +1 on an int *, it jumps sizeof (int) bytes, if we do +1 of a char * we jump sizeof (char) which is 1, so we can access byte by byte). Therefore x_arr[0], x_arr[1], x_arr[2], x_arr[3] will individually access the four bytes of the integer stored in memory (taking sizeof (int) as 4 bytes).

Therefore if we have a Little Endian system then x_arr[0] will contain 01 and if we have a Big Endian system then x_arr[0] will contain 00 . Below an implementation is shown.

Sourcecode

#include <stdio.h>

int main (void)
{
  unsigned int x = 1;
  char *x_arr = (char *) &x;

  if (x_arr[0] == 1)
  {
    printf ("Little Endian System\n");
  }
  else if (x_arr[0] == 0)
  {
    printf ("Big Endian System\n");
  }
  return 0;
}

And here is the same one in a compact form (fun!!).

#include <stdio.h>

int main (void)
{
  unsigned int x = 1;
  (((char *)&x)[0]) ? printf ("Little Endian System\n") : printf ("Big Endian System\n");
  return 0;
}
Advertisements

One thought on “Detect Endianness of a System

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s