Bitwise operators and flags
&, |, ^, ~, << and >> work on 64-bit Integers with C semantics, and on nothing else.
They're mainly for combining flag constants, testing bits in values that builtins return, and
unpacking colors.
| Task | Expression |
|---|---|
| Combine flags | FileNotify.FileName | FileNotify.LastWrite |
| Test a flag | (mask & FileNotify.Size) != 0 |
| Set a flag | mask = mask | flag; |
| Clear a flag | mask = mask & ~flag; |
| Toggle a flag | mask = mask ^ flag; |
| Test bit n | ((value >> n) & 1) == 1 or (value & (1 << n)) != 0 |
| Even / odd | (n & 1) == 0 |
Unpack 0xRRGGBB | r = (c >> 16) & 0xFF; g = (c >> 8) & 0xFF; b = c & 0xFF; |
Key held (from KeyboardGetKeyState) | (state & 0x8000) != 0. The high bit of the Win32 SHORT is sign-extended, so state < 0 works too. |
| Key toggled (Caps Lock and so on) | (state & 1) != 0 |
DisplayGetPixelColorFromPoint returns 0xRRGGBB packed into an Integer, or -1 when the point
isn't on any monitor.