Thread: Big and little endian

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Big and little endian

    Ok, Big and little endians have got me confused.

    The number 258, in memory, is (assuming a four-byte integer) 02010000 in hex on a little endian machine, and 00000102 on a big endian machine. Right?

    Now, if that's right, is the following portable between big & little endian machines?
    Code:
    unsigned int a = 258, x;
    // Output each byte of A
    for(x = 0; x < 4; x++) printf("%02X\n", (a >> (x * 8)) & 0xFF);
    As in, would that output:
    Code:
    02
    01
    00
    00
    On both big and little endian machines? Or will it be reversed on big endian machines?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    When dealing with values, endianness does not come into play. It is only when you are looking at the underlying storage of a multi-byte object that endianness is seen.

    In the code you posted, you are not looking at the underlying storage, you are manipulating a value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    In addition to Dave's post, it will make a difference if you do something like:
    Code:
    int val = 258;
    char bytes[4];
    
    bytes[0] = *(char *)val;
    bytes[1] = *((char *)val + 1);
    bytes[2] = *((char *)val + 2);
    bytes[3] = *((char *)val + 3);
    The bytes array will end up with different values on big and little endian machines. Bit-shifting a 4-byte value makes no difference though, as Dave pointed out.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Quote Originally Posted by Cactus_Hugger
    Ok, Big and little endians have got me confused.

    The number 258, in memory, is (assuming a four-byte integer) 02010000 in hex on a little endian machine, and 00000102 on a big endian machine. Right?
    wouldnt that be a 00001020 on big endian? The rest is like Lurking Cat said
    Last edited by blackswan; 10-12-2005 at 06:58 PM.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by blackswan
    wouldnt that be a 00001020 on big endian? The rest is like Lurking Cat said
    No, endianness refers to byte order. There are two hexadecimal digits to a byte. 02 01 00 00 -> 00 00 00 01 02

Popular pages Recent additions subscribe to a feed