Endianness (again)

Discussion in 'C Programming' started by gamehack, Jan 22, 2006.

  1. gamehack

    gamehack Guest

    Hello all,

    Sorry for asking too much but I've been playing with some code and just
    wrote this function:

    void reverse(uint32_t num)
    uint8_t bytes[4];
    printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
    sizeof(uint8_t), CHAR_BIT);
    bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
    bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
    bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
    bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

    printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
    bytes[3], bytes[2], bytes[1], bytes[0], num);

    And on my little endian machine it outputs:

    sizeof(uint8_t) is 1 and bits per byte is: 8
    Number in memory: 0xaa 0xbb 0xcc 0xdd
    Actual num is: 0xaabbccdd

    Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
    some really stupid mistake but I can't see it.

    gamehack, Jan 22, 2006
    1. Advertisements

  2. There is nothing in your code which relies on endianess!

    You are explicity storing 8-bit chunks of the value in num to bytes and
    then printing them out (in reverse order which accounts for your

    If you looked at the memory at &num then you would see 0xdd 0xcc 0xbb

    uint8_t *bytes = (uint8_t*)#

    printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
    bytes[0], bytes[1], bytes[2], bytes[3], num);

    Lucien Kennedy-Lamb
    Lucien Kennedy-Lamb, Jan 22, 2006
    1. Advertisements

  3. The second case in my reply to Abhishek "How to determine the data is stored
    in memory" 1/22/06 12:52 pm, might help you solve your problem..

    Rod Pemberton
    Rod Pemberton, Jan 22, 2006
  4. gamehack

    gamehack Guest

    Yeah! I got it :) I just didn't get the address of the actual bytes.


    gamehack, Jan 22, 2006
  5. gamehack

    Nelu Guest

    Please don't top-post.
    Nelu, Jan 22, 2006
  6. gamehack

    CBFalconer Guest

    printf("Number in memory: %#x %#x %#x %#x\nActual num is:
    bytes[0], bytes[1], bytes[2], bytes[3], num);

    Better yet, use:

    printf("Number in memory:");
    for (i = 0; i < 4; i++) printf(" %x", bytes);
    printf("\nActual num is: %#x\n", num);

    Long rambling statements are not conducive to accuracy.

    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    CBFalconer, Jan 22, 2006
  7. You are writing a uint32 to a sequence of uint8_t in a portable(to the
    implementations that provides these types) way in big endian
    representation. It will output the same thing on a big endian

    Great !
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Jan 23, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.