Archived

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

Could someone explain this macro?

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

Found this macro in a section of "OpenGL Game Programming."
#define KEY_DOWN(vKey)  (GetAsyncKeyState(vKey) & 0x8000) ? true : false
 
I understand what it does, could someone explain how it works? I'm expecially curious about the "0x8000" part. It looks kind of like a memory address, but I'm not exactly an expert so I don't know. I'm curious because I implemented this into my program and it screwed up something completely unrelated, so I'm guessing either it screwed up something in memory or I introduced a pointer error of some sort. [edited by - EvilProgrammer on November 10, 2003 8:56:20 PM]

Share this post


Link to post
Share on other sites
& is used for a bitwise AND; here it is used if the most significant bit of a 16-bit word is set (and thus has nothing to do with pointers or addresses). Read up on bitwise logic (and binary/hexadecimal number representation if need be)!

Edit: That's what I get for posting without thinking: 0x8000 is, of course (as was corrected below) a 16-bit value, not a 32-bit value as I originally (and idiotically) claimed.

[edited by - Miserable on November 10, 2003 10:01:59 PM]

Share this post


Link to post
Share on other sites
GetAsyncKeyStats sets the highest bit of the returned WORD if the key is pressed, 0x8000 = 1000 0000 0000 0000 binary, the bitwise operator & does a ''bitwise and'' like so...

0101...
1000...
&___
0000 key not pressed

1101...
1000...
&___
1000 pressed

? is called the ternary operator

Share this post


Link to post
Share on other sites
<< I''m especially curious about the "0x8000" part. It looks kind of like a memory address, but I''m not exactly an expert so I don''t know. >>

Its a bit-wise AND (&). The 0x8000 is binary I believe. You are masking the high bit to check for a key (or a VK code). The "?" is a conditional operator, kinda shorthand "if" statement.

Explained here

Game Programming Genesis, Part 3

Phil P

Share this post


Link to post
Share on other sites
It calls a function called GetAsyncKeyState, passes as an argument the id of the key you want to test, and checks to see if it''s down. If it is, then the hi bit will be set, otherwise it wont. Like Miserable said, the & is used in a bitwise AND, and the ? is kinda like an IF statement, and : is used to seperate the results, depending on whether the IF is true. hope that clears it up 4 ya



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

Share this post


Link to post
Share on other sites
0x8000 is just another notation for writing numbers. It's base 16, aka hexadecimal. As Miserable said read about bitwise operations and binary/hexadecimal numbers to understand how it works. After that read the MSDN docs for GetAsyncKeyState to see why the code does what it does.

And by the way, 0x8000 is a 16 bit value, not 32 bit.

Edit: Damn I'm slow

[edited by - Dobbs on November 10, 2003 9:14:12 PM]

Share this post


Link to post
Share on other sites