#### Archived

This topic is now archived and is closed to further replies.

# getting 8 bits from a 32 bit variable

This topic is 5225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, i got a 32bit variable...how do i separate them into 4 individual 8 bit (char) variable? thanks.

##### Share on other sites
Bitwise operators.

Indeed

##### Share on other sites
i know...but how? i tried shifts and it didn''t work either...

##### Share on other sites
quote:
Original post by _nomad_
i know...but how? i tried shifts and it didn''t work either...

Did you try this?
    int x32 = 0xaabbccdd;    int a = ( x32 >> 24 ) & 0xff;    int b = ( x32 >> 16 ) & 0xff;    int c = ( x32 >>  8 ) & 0xff;    int d =   x32         & 0xff;
Or this?
    int x32 = 0xaabbccdd;    char a = ( x32 >> 24 );    char b = ( x32 >> 16 );    char c = ( x32 >>  8 );    char d =   x32;
(Assuming ints are 32 bits and chars are 8 bits, of course)

John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2

##### Share on other sites
Or this:
char a, b, c, d;int x32 = 0xaabbccdd;_asm {   mov eax, x32   mov d, al   mov c, ah   shr eax, 16   mov b, al   mov a, ah}

Bitwise operator

##### Share on other sites
That only works in C/C++. And since the OP didn''t provide a target language... that''s non-compliant code

##### Share on other sites
Commented version to educate OP on instructions used, so that he can more easily port it to target processor. Kthx.
char a, b, c, d;int x32 = 0xaabbccdd;_asm {          //start inline asm block   mov eax, x32 //copy 32-bit data into 32-bit general-purpose                 //register named eax.   mov d, al    //copy the lower byte of the least significant                //word of register eax (lower word = ax, btw)                //into memory at d.                //(think, al = "lower").   mov c, ah    //Do the same as above, but with high byte of ax.                //(think, higher)   shr eax, 16  //B/c there's no register for the high word of                 //eax (ax is the low word), we must shift the                 //bits over 16 places, losing the lower word.                //shr stands for shift logical right; zeros are                //shifted in on the left, giving you the unsigned                //value aabb in ax.   mov b, al    //The contents of aabb are now in lower word,                //register ax, so do the same as you did for                  //ccdd.   mov a, ah    //finally copy the high byte of ax into a.}

[edited by - temp_ie_cant_thinkof_name on May 31, 2004 12:23:29 AM]

Use a union.

##### Share on other sites
quote:
Original post by JohnBolton
quote:
Original post by _nomad_
i know...but how? i tried shifts and it didn't work either...

Did you try this?
    int x32 = 0xaabbccdd;    int a = ( x32 >> 24 ) & 0xff;    int b = ( x32 >> 16 ) & 0xff;    int c = ( x32 >>  8 ) & 0xff;    int d =   x32         & 0xff;
Or this?
    int x32 = 0xaabbccdd;    char a = ( x32 >> 24 );    char b = ( x32 >> 16 );    char c = ( x32 >>  8 );    char d =   x32;
(Assuming ints are 32 bits and chars are 8 bits, of course)

John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2

i was doing that (the 2nd one)...but libpng saves the image wrongly...you see, it is not that i do not know bitwise operators, it is that the bitwise shifting was giving me wrong results...and so i asked how would you guys do it...and since it seems i'm doing it right on the shifting part...then there must be something wrong with my using of libpng to write png...

thanks anyway...

edit: btw, does anyone know how to convert sdl_surface into the row_pointer of libpng? thanks.

[edited by - _nomad_ on June 1, 2004 4:17:29 AM]

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 13
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007290

×