Wednesday, April 4, 2012

What is big-endian and little-endian?

Endian ness is used to specify the order in which a sequence of bytes (not bits) are stored in a computer memory. There are two types of endianness namely big-endian and little-endian. Big-endian is an order in which most significant byte(MSB) is stored first. Littl-endian is an order in which least significant byte(LSB) is stored first. For example two byte hexadecimal code 4F5E will be stored as 4F5E in big-endian. In little endian systems it will be stored as 5E4F. Lets more clearly.

Generally memory is a collection of bytes. Each byte is collection of eight bits. So data will be stored in the memory byte by byte. This storage criteria depends on the machine architecture. But bits with in the byte is always same. Even though data storage in the memory is depends on the machin architecture, there should be some common point to uderstand and to communicate with other machines. below is the one

  • A bit has a value (on or off , zero or one )
  • A byte is sequence of eight bits
    • the left most bit in the byte is biggest. So the binary value 00001011 decimal value is 11 (8+2+1 = 11).  
    • Bits are numbered from right to left.  Bit zero is the right most and smallest. Bit seven is left most and largest.
So above are the common in all machines. Storing data in the byte is common in all machines. If you are manipulating the memory with one byte data (like char in C). There wont be any problem. Because storage process for bits in byte is common to all machines. But if are manipulating the data with multiple bytes (like int, float in C), there comes the problem. Lets see the byte ordering and storage.

Byte ordering:
Assume that there are sequnce of 4 bytes namely W, X, Y, Z. And their corresponding values are given in the given table in hexadecimal. i.e byte W address is zero and value is 0x12 in hexa (00010010 in binary). similarly for other bytes X,Y, Z are shown below.

Byte Name:    W       X       Y       Z
Location:     0       1       2       3
Value (hex):  0x12    0x34    0x56    0x78

Assume that there is a two byte XY, its corresponding value si 3456 in hexadecimal. So reading this data in both endians in the following way.
Big-endian: stores data big-end first. So it starts reading from big-end, here it is X as a first byte and its value is 0x34 and Y as  second byte and its value is 0x56.
Little-endian: stores data little-end first. So it starts reading from little-end, here it is Y as a first byte and its value is 0x56 and X as a second bytes and its value is 0x34.

IBM's 370 mainframes, most RISC-based computers, and Motorola microprocessors use the big-endian approach. TCP/IP also uses the big-endian approach. So big-endian is also called network order.On the other hand, Intel processors (CPUs) and DEC Alphas little-endian.


Samplec code to find the endianness of the machin in C:
main()
{
    int i=1;
    //typecasting int to char to read the first byte value
    if(*(char *)&i == 1)
        printf("Little endian\n");
    else
        printf("Big endian\n");
}

OutPut:
Little endian

In the above code,  initialised the int variable i with value one. In C generally memory required to store int is either 2 or 4 bytes. Here in our machine it was 4 bytes. In the if statement , we tried to read the first byte by typecasting it to char * (as char need only one byte to store) and checking that value with assigned value. If it is little endian least byte would be value one, if it is big endian most significant byte value would be one.

No comments:

Popular Posts