Thread: endianness

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Plano, Texas, United States

    endianness kind of confused...

    i am sitting here at work, kind of bored. I have finished all my work, so I decided to program for awhile just for fun.

    But anyways, as most of us know, in a big endian machine, the most significant byte is stored lower in memory, so the integer FFFF would be stored as such in memory:

    00 00 FF FF

    In a little endian machine, the least significant byte is store lower in memory, and so that same number would be stored as such:

    FF FF 00 00

    Well, my machine here at work is a Win2k machine...running on an Intel processor, and therefore it is little endian because Intel processors are little endian. Or at least according to this website they are:

    Well, what confuses me is this. I outputted the unsigned integer FFFF bit by bit from left to right, and it came out like this:

    00000000 00000000 11111111 11111111

    All i did was bitshift and output in a loop. Well, since intel machines little endian, isnt that backwards? shouldnt it be outputting:

    11111111 11111111 00000000 00000000

    or am i just not thinking straight right now.....

    maybe this should have gone on the tech board...i dunno...i just posted it here...but if it should be on the tech board feel free to move it there
    My Website

    "Circular logic is good because it is."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Within the confines of a single program, endian-ess is pretty much a non issue. It does not matter to your program whether FFFF is stored as FFFF0000 or 0000FFFF, because it will always have the value FFFF. So when you output the bits, you get what you expect.

    unsigned long foo = 0xFFFF;
    fwrite( &foo, sizeof foo, 1, fp );

    Take this file to another machine, one with a different endian and repeat your exercise. Then you'll see what endian means.
    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
    Registered User
    Join Date
    Aug 2003
    You're probably not doing the printing at the byte level. You should cast a pointer to the integer to unsigned char* and then print out the bytes in sequence.

  4. #4
    Registered User
    Join Date
    Apr 2004
    "One little, two little, three little endians.."

    Sorry, I had to..

  5. #5
    Registered User
    Join Date
    Jan 2002
    Endianness doesnt concern individual bits, it affects bytes as a whole. So when you print out the individual bits in order from 0 - 31 you will get them in that order no matter what the endian of the machine is. In the number 0x0001 for instance, 1 is always in bit position 0.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Endianness and bit shifting
    By synthetix in forum C Programming
    Replies: 27
    Last Post: 06-07-2009, 01:06 AM
  2. Changing endianness
    By slippy in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 01:17 PM
  3. Endianness programme help
    By rits in forum C Programming
    Replies: 8
    Last Post: 03-17-2009, 04:32 PM
  4. Endianness - Is this correct?
    By Tommo in forum Tech Board
    Replies: 2
    Last Post: 01-06-2008, 12:56 PM
  5. Endianness conversion macros
    By BrownB in forum C Programming
    Replies: 21
    Last Post: 12-10-2004, 09:12 AM