Endianness (again)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gamehack

    #1

    Endianness (again)

    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.

    Thanks

  • Lucien Kennedy-Lamb

    #2
    Re: Endianness (again)


    gamehack wrote:[color=blue]
    > 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.
    >
    > Thanks[/color]

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

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


    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

    Comment

    • Rod Pemberton

      #3
      Re: Endianness (again)


      "gamehack" <gamehack@gmail .com> wrote in message
      news:1137957460 .676027.84780@g 44g2000cwa.goog legroups.com...[color=blue]
      > 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.
      >
      > Thanks
      >[/color]

      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


      Comment

      • gamehack

        #4
        Re: Endianness (again)

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

        Thanks


        Rod Pemberton wrote:[color=blue]
        > "gamehack" <gamehack@gmail .com> wrote in message
        > news:1137957460 .676027.84780@g 44g2000cwa.goog legroups.com...[color=green]
        > > Hello all,[/color][/color]
        [snip][color=blue]
        >
        > 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[/color]

        Comment

        • Nelu

          #5
          Re: Endianness (again)

          gamehack wrote:[color=blue]
          > Yeah! I got it :) I just didn't get the address of the actual bytes.
          >
          > Thanks
          >[/color]
          Please don't top-post.
          --
          Ioan - Ciprian Tandau
          tandau _at_ freeshell _dot_ org (hope it's not too late)
          (... and that it still works...)

          Comment

          • CBFalconer

            #6
            Re: Endianness (again)

            gamehack wrote:[color=blue]
            >
            > 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.[/color]

            Try:
            printf("Number in memory: %#x %#x %#x %#x\nActual num is:
            %#x\n",
            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[i]);
            printf("\nActua l num is: %#x\n", num);

            Long rambling statements are not conducive to accuracy.

            --
            "If you want to post a followup via groups.google.c om, 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/>

            Comment

            • Nils O. Selåsdal

              #7
              Re: Endianness (again)

              gamehack wrote:[color=blue]
              > 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.[/color]

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

              Great !

              Comment

              Working...