Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

03
Nov

Little, Big endianess explained

Related Blog Items

Big endian and little endian
"Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte at the highest address. (The little end comes first.) For example, a 4 byte Long integer

    Byte3 Byte2 Byte1 Byte0

will be arranged in memory as follows:

   Base Address+0  Byte0
   Base Address+1  Byte1
   Base Address+2  Byte2
   Base Address+3  Byte3

Intel processors (those used in PC’s) use "Little Endian" byte order.

"Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. (The big end comes first.) Our LongInt, would then be stored as:

  Base Address+0  Byte3
  Base Address+1  Byte2
  Base Address+2  Byte1
  Base Address+3  Byte0

Motorola processors (those used in Mac’s) use "Big Endian" byte order.

Which is Better?

You may see a lot of discussion about the relative merits of the two formats, mostly religious arguments based on the relative merits of the PC versus the Mac. Both formats have their advantages and disadvantages.

In "Little Endian" form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first pick up the lowest order byte at offset 0. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write.

In "Big Endian" form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. You don’t have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient.

Ex :

1024 (0×00000400) stored in little endian machine

as

00 00 04 00

in big endian machine stored as

00 04 00 00

is it possible to find endianness at compile time?

no, it is not possible. but we can use compile time flags

ex.

#ifdef _BIG_ENDIAN

//this code

#else

//this code

#endif

How to find endianness by a program

int x=1 ;if(*(char *) &x == 1)

 

printf("little endian") ;

else

pritf("big endian") ;

[tags]big endian, little endian, endianness, embedded system programming[/tags] 

 

Popularity: 36%

You need to log on to convert this article into PDF


Related Blog Items

3 Comments

  • OpenAsthra » Blog Archive » Little, big endianess explained — part2  said:

    […] Byte order utililities: Previous post little, big endianness explained deals with theoretical part of endianness, now let us write some utility functions for endainness. […]


  • Sridhar  said:

    Please correct this !!
    Indicate which address is lower and which is Higher address.
    By writing simply its giving impression that example shown is wrong for big and little indian cases.

    Ex :

    1024 (0×00000400) stored in little endian machine

    as

    00 00 04 00

    in big endian machine stored as

    00 04 00 00

    —–Siddhuu Siddhartha Roy (Bengal Tiger)


  • ponnada  said:

    [snippet]
    Big endian and little endian
    "Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte at the highest address. (The little end comes first.) For example, a 4 byte LongInt

    Byte3 Byte2 Byte1 Byte0
    [/snippet]

    in the above theory I’d mentioned that lower order byte is stored at lowest address for little endian and gave pictorail representation of memory arrangement, so it should not cause any confusion, I guess.
    anyways

    1024 represented as below in little endian machine

    lowest address
                    |
                    V
    00 00 04 00

    in big endian it will be represented this way
    highest address
    |
    V
    00 04 00 00

    thanks siddhuu :-)


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image