Wednesday, August 29, 2007

Little Endian, Big Endian & Gulliver ..

Big Endian and Little Endian are terms used in computing to specify how bytes are ordered in memory. In Big Endian bytes are stored from most significant byte to least significant byte (MSB to LSB) and in Little Endian it is from least significant byte to most significant byte (LSB to MSB) .
For example,on a 32bit system an unsigned integer ‘0x40812131’ will be stored as ‘0x40812131’ itself in Big Endian byte order and in Little Endian, it will be stored as ‘0x31218140’.

Most Intel processors are Little Endian, while processors such as Sparc belong to the Big Endian family. In processors like MIPS and ARM it is possible to specify the byte order at startup.

TCP/IP uses Big Endian byte order for its data transfer. Systems with a byte order other than BigEndian should first convert any data it receives from a TCP/IP network (Network Byte Order) to its own byte order (Host Byte Order) before processing it. Most network appliances (routers, firewalls,ids,ips,..) are build using BigEndian processors as they can avoid overhead caused due to byte order conversions.
Following are some C functions used for byte order conversion

unsigned long htonl(unsigned long hostlong); //Host byte order to Network byte order long
unsigned short htons(unsigned short hostshort);//Host byte order to Network byte order Short
unsigned long ntohl(unsigned long netlong);//Network byte order to Host byte order long
unsigned short ntohs(unsigned short netshort); Network byte order to Host byte order short


These functions are even found in the source code for BigEndian systems, but internally they don’t perform any operation on them. This is mainly done to make the code portable across platforms with different byte orders.
One interesting fact about the terms ‘Little Endian ‘and ‘Big Endian’ is that they came from the novel Gulliver's Travels :) .


"Gulliver finds out that there is a law, proclaimed by the grandfather of
the present ruler, requiring all citizens of Lilliput to break their
eggs only at the little ends. Of course, all those citizens who broke
their eggs at the big ends were angered by the proclamation. Civil war
broke out between the Little-Endians and the Big-Endians, resulting in
the Big-Endians taking refuge on a nearby island, the kingdom of
Blefuscu."
[http://www.ietf.org/rfc/ien/ien137.txt]

The following code could be used to check whether a system is Big Endian or Little Endian during run time.




#include
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
int byteOrder(void)
{
unsigned int x=1;
unsigned char *y=(unsigned char *)&x;
if(y[0]) return LITTLE_ENDIAN;
return BIG_ENDIAN;
}
int main()
{
printf("%s Endian\n",byteOrder()?"Big":"Little");
return 0;
}

1 comments:

Phil said...

Pretty crazy about the whole Gulliver thing isnt it?