Endianness (again)

G

gamehack

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
 
L

Lucien Kennedy-Lamb

gamehack said:
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

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
 
R

Rod Pemberton

gamehack said:
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

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
 
G

gamehack

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

Thanks


Rod said:
gamehack said:
Hello all,
[snip]

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
 
C

CBFalconer

gamehack said:
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.

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);
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/>
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

gamehack said:
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.

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 !
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top