Sign in to follow this  
Ne0

Getting the High-Order bit

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this