Search code examples
c#.netcapslock

Why is & 0xFFFF needed when checking if caps lock on


I'm trying to check if CAPS LOCK is on. I have seen the following line and I was wondering why is the AND with 0xFFFF needed since AND with 0xFFFF will return exactly the same number. I have read here that in order to check if the CAPS LOCK is toggled, you need to check the lower-order bit hence it should be AND with 1. So, why 0xFFFF?

bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

Thanks!


Solution

  • I aggree with GSerg that it is not needed to be used. But normally the use of and bitwise operator is to get portion of the value.

    for instance:

    0x11ffff
    0xffff 
    --------- AND
    0xffff
    

    You can use this online bitwise calculator to understand how it works.