How do I get the high- and low-order bits of a SHORT?

Dylan picture Dylan · Mar 14, 2011 · Viewed 18.5k times · Source

The function GetKeyState() returns a SHORT that contains the key's state (up/down in the high-order bit, and toggled in the low-order). How do I get those values?

Answer

ChrisV picture ChrisV · Mar 14, 2011

Simple bit manipulation will work. SHORTs are 16-bit integers, so to get the low- and high-order bits you can do the following:

lowBit = value & 1;
highBit = ((unsigned short) value) >> 15;

Also, note that the LOBYTE and HIBYTE macros are used to break SHORTs into low- and high-order bytes, not to test individual bits in a byte.