Breaking up a LPARAM variable & looking at groups of bits

前端 未结 4 1954
死守一世寂寞
死守一世寂寞 2021-01-16 23:52

I know that a LPARAM variable has certain bits set(inside it) that identify information such as long key presses & etc. when I receive a WM_KEYDOWN event.

So I a

4条回答
  •  粉色の甜心
    2021-01-17 00:41

    In general, you use bitwise-AND to check if a certain bit is set:

    unsigned int flags;  // some flags
    
    if (flags & 0x01) { } // bit 0 is set
    if (flags & 0x02) { } // bit 1 is set
    if (flags & 0x04) { } // bit 2 is set
    ...
    if (flags & (1U << n)) { } // bit n is set
    

    However, don't rely on the physical bit values. Instead, the API defines USEFUL_CONSTANTS that describe the meaning of the flags:

    LPARAM flags = ApiFunction();
    if (flags & USEFUL_CONSTANT) { } // check if the flag is set
    

    Check the API documentation of the relevant message to find out which values are defined.

    Update: I see that in your case you might actually want values rather than just flags. So, to get the value of the lowest 16 bits, you just bitwise-AND the value with the corresponding bitmask: unsigned int repeat_count = flags & 0xFFFF; Note that 0xFFFF is 1111111111111111 in binary.

提交回复
热议问题