endianness

This is a discussion on endianness within the Tech Board forums, part of the Community Boards category; okay...im kind of confused... i am sitting here at work, kind of bored. I have finished all my work, so ...

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

    endianness

    okay...im 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: http://www.netrino.com/Publications/...ndianness.html

    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.....

    [edit]
    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
    [/edit]
    My Website

    "Circular logic is good because it is."

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    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
    Posts
    100
    "One little, two little, three little endians.."

    Sorry, I had to..

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    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, 11:56 AM
  5. Endianness conversion macros
    By BrownB in forum C Programming
    Replies: 21
    Last Post: 12-10-2004, 08:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21