if (GetAsyncKeyState(key) & 0x8000)
What is the meaning of "&" in the if statement above? Why we need to use it?
if (GetAsyncKeyState(key) & 0x8000)
What is the meaning of "&" in the if statement above? Why we need to use it?
GetAsyncKeyState(key)
will return you 16-bit value.
Such 1010010101110110
The highest order bit contains the key state.
1010010101110110 highest order bit in bold.
Since we are only interested in the value on the highest order and mask the rest, we do the following operation &
bitwise and operation.
0x8000 is hexadecimal represendtation of 1000000000000000.
==>
Result:
As the example shows, we mask the bits other than the highest order bit which is set if the key is pressed otherwise zero. If block will be executed if the key is pressed.