Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

hi and lo byte

This topic is 4890 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, how can i save two values in one variable? i think this shoudl be possible to set the hi byte and the lo byte in a 16 bit integer, right? how can i set the hi and lo byte? thanks!

Share this post


Link to post
Share on other sites
Advertisement
Well, if you want to pack 2 bytes into a 16 bit short, you could do it like this:
unsigned short Value = HI << 8 + LO;

It's a simple matter of shifting up the higher bytes to the right places. To get the values back:
unsigned char LO = Value & 0xff; unsigned char HI = (Value & 0xff00) >> 8;

I'd question whether this is a good idea, but I can't really say without knowing what you're trying to do.

Share this post


Link to post
Share on other sites
thank you!
yes its an unsigned short (UInt16).

i think that's exactly what i need.

another question:
does something like this

x & 0xff

perform a modulo 255 operation to x?
if yes, than what is the difference to just writing x % 255 ?

thank you!

Share this post


Link to post
Share on other sites

unsigned short Pack(unsigned char high, unsigned char low) {
return ((high << 8) + low);
}
unsigned char UnpackLow( unsigned short value ) {
return (value & 0xFF);
}
unsigned char UnpackHigh( unsigned short value ) {
return ((value & 0xFF00) >> 8);
}


edit: bah, Promit posted while I was writing :(

Share this post


Link to post
Share on other sites
Quote:

x & 0xff

perform a modulo 255 operation to x?
if yes, than what is the difference to just writing x % 255 ?


& and % are two very different things. & is the logical AND operation(| is OR).

AND works like this:

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

so if x = 1011001100011101 and you wanted the low byte:

1011001100011101
0000000011111111 (& 0xFF)
----------------
0000000000011101 = Low byte(top 8 bits will always be 0)

so if x = 1011001100011101 and you wanted the high byte:

1011001100011101
1111111100000000 (& 0xFF00)
----------------
1011001100000000 = Shift over 8 bits and get the high byte

Edit: haha nice try _DarkWIng_

Share this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
does something like this

x & 0xff

perform a modulo 255 operation to x?
if yes, than what is the difference to just writing x % 255 ?

This is bitwise and. It performs and on each bit and it basically one of the atomic operations of CPU.


A B | A&B
----+----
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1


ps: to late.. AGAIN!

Share this post


Link to post
Share on other sites
After some testing it seems theres no need it use & to get the high byte, just shifting will work.

unsigned char UnpackHigh( unsigned short value ) {
return (value>>8);
}

or

unsigned char HI = (Value >> 8);

This is probably because C++ uses shift, not rotate.

Share this post


Link to post
Share on other sites
ah, i think i got it!

thanks guys!

so with something like

unsigned short value = ( (shortA & 0xff) << 8) | (shortB & 0xff);

i can write shortA and shortB in the hi and lo byte of value?
(they are both positive)

Share this post


Link to post
Share on other sites
Well you can't store two shorts in one and having two seperate shorts just to store one byte each is kind of silly unless you need to test for overflows on math operations(ex. writing an emulator).

Use Darkwing's function too pack two bytes into one short:

unsigned short Pack(unsigned char high, unsigned char low) {
return ((high << 8) | low);
}

unsigned short Value=Pack(0x65,0xF3); //Value=0x65F3

(| is faster than +)

Share this post


Link to post
Share on other sites
Then of course there exists the ugly, but slightly faster version which takes advantage of the addressing:


unsigned char a,b;
unsigned short c;
/* --- variable setup here --- */

*((unsigned char *)&c) = b;
*(((unsigned char *)&c)+1) = a;



It turns out to be aproximately 20% faster (ie. takes 20% less time to execute) when tested with a loop construct on a Athlon when compiled with gcc 3. Of course simple loop-test doesn't tell the whole truth and obviously this kind of micro-optimization is next to pointless. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!