Thread: Big Endian & Little Endian

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    Big Endian & Little Endian

    Hi Guys,
    How to find out the given processor is a big endian or small endian.??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read it's reference manual ?

    http://c-faq.com/misc/endiantest.html

    What about those processors which are neither?

    What about those processors which can change endian on the fly?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hmmm, what is an Endian? i thought he meant indian or sumthin lol!
    can anybody give me a brief description of what an endian is? thanks!
    It is not who I am inside but what I do that defines me.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Haven't you ever heard of a search engine? Or are you just too lazy? [/rhetorical]


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    Thumbs up

    Try this code to find out your processor is big or small endian

    Code:
    main()
    {
       int num = 1;
       if(*(char *)&num == 1)
       {
          printf("\nLittle-Endian\n");
       }
       else
      {
          printf("Big-Endian\n");
      } 
    }

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

    For example, a 4 byte, 32-bit 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


    Example :Intel processors use "Little Endian" byte order.


    "Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.


    Base_Address+0 Byte3
    Base_Address+1 Byte2
    Base_Address+2 Byte1
    Base_Address+3 Byte0


    Motorola, Solaris processors use "Big Endian" byte order.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Mips?
    Arm?
    Pdp-11?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    ok. i think i got the general idea. now how does this endian thingy work? i mean, wuts the difference between using big and lil endian byte orders?
    It is not who I am inside but what I do that defines me.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    wuts the difference between using
    Using for what?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > wuts the difference between using big and lil endian byte orders?
    For 99% of the programs, none at all.

    For anything involving network communication, there are functions like htonl() and ntohl() which convert between network byte order and the byte order of the local machine.

    The only other place you're likely to come across it is say picking apart image files (say .bmp) which store sizes in a specific byte order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    Hi,
    I was searching for the same problem, and this thread proved to be very helpful. Thanks to everyone.
    I have just one more query, how can I find whether the computer is little endian or big endian using a union.

    I fully understand the code provided by Swaugh (thanks for it) but I also need to find out how I can accomplish the same using a union.

    Thanks in anticipation,
    regards,
    koodoo

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't bump old threads, especially a thread as old as this one. You can start a new thread and link to an old thread if you want to.

    BTW, it's [color=red]text[/color], not [red]text[/red].

    I have just one more query, how can I find whether the computer is [red]little endian[/red] or [red]big endian[/red] using a union.
    This might work, though I'm not too sure about it:
    Code:
    union {
        short snum;
        unsigned char cnum;
    } test;
    
    test.snum = 1;
    if(test.cnum == 1) puts("little edian");
    else puts("bug edian");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28

    Thumbs up

    Quote Originally Posted by dwks View Post
    You shouldn't bump old threads, especially a thread as old as this one. You can start a new thread and link to an old thread if you want to.
    I'll remember that, the next I have such a query.

    Quote Originally Posted by dwks View Post
    BTW, it's [color=red]text[/color], not [red]text[/red].
    Thanks for that too, actually I'm used to some other forums where that kinda thing works

    Quote Originally Posted by dwks View Post
    This might work, though I'm not too sure about it:
    Code:
    union {
        short snum;
        unsigned char cnum;
    } test;
    
    test.snum = 1;
    if(test.cnum == 1) puts("little edian");
    else puts("bug edian");
    I think it will thanks for the help. I must also apologize coz I posted so very early (without putting in much effort myself, I was kinda in a hurry).
    I searched and found a similar solution.
    The following code :
    Code:
    #include <stdio.h>
    
    void main()
    {
            union s
           {
                int i;
                char ch[2];
           };
    
    
    union s obj;
    obj.i=256;
    printf("%d %d %d\n",obj.i,obj.ch[0],obj.ch[1]);
    
    }
    would print 256 0 1 on a lil endian machine, while 256 1 0 on a big endian machine.

    Thanks for all the help

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The code that you found has a few problems with it. First of all, void main() is a bad idea, and one should use int main(). Secondly, I'm not sure if it would work when ints were different sizes or chars were signed versus unsigned.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    root koodoo's Avatar
    Join Date
    Oct 2005
    Location
    a small village faraway in the mountains
    Posts
    28
    yeah, thanks again.
    I don't know why I am in such a hurry and messing up things.
    In the discussion from where I got this code, the first error pointed out was that of using void main() which of course is a bad practise, I forgot to correct it while copying and pasting code.
    Moreover, the link posted earlier in this thread http://c-faq.com/misc/endiantest.html
    describes both the methods (using pointers & unions) It deals with varied sized ints, however it should have chosen unsigned char as you have suggested.

    P.S. On more query, this might be a bit silly, but I could not find how to add a signature to my profile. I tried the USER CP and edit profile links, but it was not there...I feel completely stupid as of now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any significant Big Endian Machines?
    By abachler in forum Tech Board
    Replies: 9
    Last Post: 01-29-2009, 01:53 PM
  2. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  3. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  4. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM