Jump to content
  • Advertisement

Archived

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

_nomad_

getting 8 bits from a 32 bit variable

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!