Endianness

From C

Jump to: navigation, search

Endianess refers to the ordering of bytes in a multi-byte number. Big endian refers to the architecture where the most significant byte has the lowest address, while the opposite Little endian, the most significant byte has the highest address.

Assuming that your machine is big or little-endian, you can find the endianess of your architecture using the following programming snippets:


int x = 1;
	if(*(char *)&x == 1)
		printf("little-endian\n");
	else	printf("big-endian\n");


#define LITTLE_ENDIAN 0
#define BIG_ENDIAN    1

int machineEndianness()
{
   short s = 0x0102;
   char *p = (char *) &s;
   if (p[0] == 0x02) // Lowest address contains the least significant byte
      return LITTLE_ENDIAN;
   else
      return BIG_ENDIAN;
}

Category Snippets

Personal tools