Search code examples
c++winapishort

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


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?


Solution

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