Skip to main content
gamedev.net
🔒 Locked

Quick question?

Started by Asheh Oct 20, 2005 at 11:10 AM 5 replies 450+ views
Original Post
Asheh
Asheh
How do i find if the LSB/MSB is set in a short? I cant remember, is it somthing like short var; if(var & 0xff) ? Cheers guys.
Nitage
Nitage
Assumming a short is 2 bytes in the format hiByte|loByte

then:

unsigned char loByte(short s){    return s & 0x00FF;}unsigned char hiByte(short s){    return (s & 0xFF00)>>8;}
Asheh
Asheh
i understand loByte, but err, whats with the bitshift on highbyte?
vNistelrooy
vNistelrooy
Quote:
Original post by Asheh
i understand loByte, but err, whats with the bitshift on highbyte?


You return a byte, so you shift the high byte to the low byte. You want to check if the high byte is set, don't you?
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
Asheh
Asheh
Doesnt seem to be working too well.

From the docs it says that the most significant bit is set the key is down. So so the person cant keep hold of the key. So if the key isnt down, it must be up?

		short key_up = GetAsyncKeyState(VK_UP);		if(((key_up & 0xFF00)) && prevSpaceState == false) {			prevSpaceState = true;			if(can_rotate()) 			{				active_piece->Rotate();			}		} else 	{			prevSpaceState = false;		}
swiftcoder
swiftcoder
Quote:
Original post by Asheh
Doesnt seem to be working too well.

From the docs it says that the most significant bit is set the key is down. So so the person cant keep hold of the key. So if the key isnt down, it must be up?

*** Source Snippet Removed ***


Not necessarily, that flag only indicates that the key has just gone down, and there is another which will show when it has just gone up. If you want to know whether the key is up or down in-between these flags being fired, you will have to keep track of it yourself.

[edit]Now that I read your snippet entirely, I realized you are all ready tracking this ;) Sorry[/edit]
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
stylin
stylin
To do it more generically, where sizes can be different on different platforms, you could use this little ditty (plus it doesn't trash your var when you test):
#include <iostream>// returns true if most significant bit is settemplate< typename bitvar >bool test_msb( bitvar bv ) {   return bv & ( 1 << ( sizeof(bv) * 8 - 1 ) );}int main() {   unsigned char c  = 0x80;   unsigned short s = 0x8000;   unsigned int i   = 0x80000000;      if( test_msb( c ) ) {      std::cout << c << "c's MSb is set" << std::endl;   }   if( test_msb( s ) ) {      std::cout << "s's MSb is set" << std::endl;   }   if( test_msb( i ) ) {      std::cout << "i's MSb is set" << std::endl;   }   return 0;}

BTW, I assume you mean most significant bit, not byte ? I noticed you were testing the most significant bit by anding a whole byte to it. Not sure, though.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.