-->

Sunday, November 30, 2014

What is little endian and big endian or What is Network byte order and host byte order ?




               Little endian and Big endian are memory architecture to store Multi byte data into memory.Say integer,float data types are occupy more then 1 byte in memory.And these are also called as Network Byte order and Host byte order.

Little endian?

      Little endian means lower order byte in number is stored lower order address in memory.
 

       For example:

                     int number = 0x1234;
                  
                     Memory Address              Value                       
                        1000                                    34           ------>   Base_Addr_0 +  Lower_order_byte;
                        1002                                    12           ------>  Base_Addr_1  +  Higher_order_byte;


Big endian?


     Big endian means higher order byte in number is stored lower order address in memory..

For example:


                   int number = 0x1234;

                     Memory Address              Value
                      
                       1000                                    12             ---->   Base_Addr_0 +  Higher_order_byte;
                       1002                                    34            ----->  Base_Addr_1  +  Lower_order_byte;
 

The above example shows memory representation of integer data type which is stored Little and Big endian machines.

How to check your PC is based on little endian or big endian Architecture?

 Here, some example given below:-

      Example 1:

             
void main(void)
                  {
                           int number = 0x1234;   // 0x represent as Hex value
                           char *ptr=(char*) & number;  
                           if(*c==0x34)
                                   {printf("Little Endian");}

                           else if(*c==0x12)
                                    {printf("Big Endian");}
                            else { printf("**************");}

                   }
     Example 2:

             
void main(void)
                      {
                             int number =1;
                             if(*(char)&number==1)printf("Little Endian");                         
                            else printf("Big Endian");
                      }
                    
    Example 3: 

              
void show_memory(unsigned char*,unsigned char);  //Function declaration.
               void main(void)

                      {
                              int number= 0x1234;
                              char *ptr =(*char)&number;
                              show_memory(*ptr,sizeof(number)); //Function Calling
                              getch(); 
                              return 0;
                       } 

                        
               void show_memory(unsigned char *p,unsigned char n)

                      {
                                for(char i=0;i<n;i++) printf("%d  ",p[i]);                      }
                      

      Note : if your machine is little endian output will be 34 12....Suppose big endian means output will be 12  34


What bi endian? 

    Bi Endian means combination of little and big endian.Bi Endian processor can run in both little and big endian.

      Example of Little and big and Bi endian architecture?
  1. Intel based processors are Little Endian.
  2. ARM based processors were Little Endian...now its became Bi-Endian.
  3. MOTOROLA processors were Big Endian...now its became Bi-Endian.


  

No comments:

Post a Comment

Receive Our Quality Tutorials Straight In Your Inbox By Submitting Your Email ID Below...

Your Information Will Never Be Shared.