Big endian and Little endian computers

来源:百度文库 编辑:神马文学网 时间:2024/05/15 22:54:11
Virtually all computer architectures are byte addressable. Ifan int is four bytes, there are two different ways to store this.Suppose the address of the int is A. In a so-called big endiancomputer, the highest order byte is stored at A, and the lowestorder byte is stored at address A+3. In a so-called littleendian computer, address A stores the least significant byteand the most significant byte is at address A+3.
Big endian computer architectures include the IBM 370,the Motorola 68000 and Sun Sparc. Little endian computersinclude the intel series (80486, pentium etc) and VAX.
Consider the decimal integer 91,329. This is 00 01 64 C1in hexidecimal. If this were to be stored at address Ain a big endian computer, 00 would be at address A,01 at address A+1 64 at address A+2, and C1 at addressA+3. On a little endian computer, C1 would be the valueat 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 whenlittle endian computers are going to pass integersover the network (IP addresses for example), they needto convert them to network byte order. Likewise, when thereceive integer values over the network, they need toconvert them back to their own native representation.
There are four functions that do this.
unsigned long htonl(unsigned long)host to network conversion for long ints (4 bytes)unsigned short htons(unsigned short)host to network conversion for short ints (2 bytes)unsigned long ntohl(unsigned long)network to host conversion for long intsunsigned short ntohs(short)network to host conversion for short intsOn big endian computers these functions simply return theirarguments.
The terms big endian and little endian are borrowed fromGulliver‘s Travels.