Jump to content
  • Advertisement
Sign in to follow this  
Ne0

Getting the High-Order bit

This topic is 4493 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

HI! Im trying to get they "High-Order bit" of a short but ive totally forgotten how to do it. More specificly, im using the GetKeyState function (MSDN Artical) and need to find weather the key is pressed or not. I remember something like...
short KeyState = GetKeyState(VK_F1);
if (HIGH(KeyState) == 1)
{
    // Whateva
}
... but i really have no idea. All I need is to know if the high order bit is 1 Any help would be much appreciated :-) Tj.

Share this post


Link to post
Share on other sites
Advertisement
Assuming a short is 16 bits on any platform you'll compile for:
short KeyState = GetKeyState(VK_F1);
if ((KeyState >> 15) == 1)
{
// Whateva
}
More flexibly, assuming a byte is 8 bits:
short KeyState = GetKeyState(VK_F1);
if (((KeyState >> (sizeof(short) * 8 - 1) == 1)
{
// Whateva
}
There are plenty of other ways to do it as well. I would personally do:
short KeyState = GetKeyState(VK_F1);
if ((KeyState & 0x8000) != 0)
{
// Whateva
}
Basically, use either bit-shifts or bit-masks, or a combination of both. (About.com : Bitwise Operators)

[edit]Ghaarrr! I type a lot.[/edit]

Share this post


Link to post
Share on other sites
The portable way:

#include <climits> // or <limits.h> in C

if(keyState & (1 << (sizeof(short) * CHAR_BIT - 1)))
{
...
}

Share this post


Link to post
Share on other sites
Woah! Thats alot of info! Thanks Antheus, Dave Hunt, Agony and Sharlin!

I ended up using the simple one (KeyState < 0)...hehe... but thanks everyone else!!

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!