Sunday, December 5, 2010

Big Endian vs Little Endian

All computers are byte addressable. For example in a 32 bit processor int is 4 bytes. There are two ways to represent this, the big endian and the little endian.

The most significant byte is stored first in the big endian. Suppose the address of the int is A. In a so-called big endian computer, the highest order byte is stored at A, and the lowest order byte is stored at address A+3. In a so-called little endian computer, address A stores the least significant byte and the most significant byte is at address A+3.

IBM, Motorola are Big endian and Intel on the other hand is little Endian. Consider the decimal integer 91,329. This is 00 01 64 C1 in hexidecimal. If this were to be stored at address A in a big endian computer, 00 would be at address A, 01 at address A+1 64 at address A+2, and C1 at address A+3. On a little endian computer, C1 would be the value at address A, 64 at address A+1, 01 at address A+2, and 00 at address A+3.

Computer networks are big endian. This means that when little endian computers are going to pass integers over the network (IP addresses for example), they need to convert them to network byte order. Likewise, when the receive integer values over the network, they need to convert them back to their own native representation.