Flags

Many functions have parameters, often called "flags", that can consists of several values (bits). To create flags from several values, use operator | (bitwise or). Examples:

 

int hwnd=win("Window" "" "" 1|8) 
SetWindowPos(hwnd HWND_TOPMOST 0 0 0 0 SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW)

 

Usually flag values are divisible by two (1, 2, 4, 8, 16, and so on). If so, the | operator is the same as the + operator. For example, to specify flags 1, 4 and 16, you can use 1|4|16, or you can use 21. Flags usually are easier to read when they are in hexadecimal format, so instead of 21 you can use 0x15. Another example: 5|0x10.

 

A flag takes 1 bit, therefore an int value can have 32 flags: 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, ... 0x80000000.

 

If flags is an optional parameter of a function, its default value is 0.

 

How to compare flags

Usually you need to know whether one of flags is set. Use operator &. Example: if(flags&8).

 

Don't use operator = (for example, if(flags=3)), usually it has no sense.

 

How to read flags from a hexadecimal number, and vice versa

QM dialogs often create code that includes flags, usually in hexadecimal format, like 0x3011. How to know what flags are encoded in the number?

 

At first, need to understand hexadecimal number format:

https://en.wikipedia.org/wiki/Hexadecimal

https://en.wikipedia.org/wiki/Flag_field

 

Each hexadecimal digit (after 0x) is a sum of max four flags at that digit position: 1, 2, 4 and 8.

 

Examples:
0x5 contains flags 0x4 (4) and 0x1 (1).
0x500 contains flags 0x400 (1024) and 0x100 (256).
0x52A contains flags 0x400, 0x100, 0x20, 0x8 and 0x2.
0x3011 contains flags 0x2000, 0x1000, 0x10 and 0x1.

Table:
Digit - flags
0 - none
1 - 1
2 - 2
3 - 1, 2
4 - 4
5 - 4, 1
6 - 4, 2
7 - 4, 2, 1
8 - 8
9 - 8, 1
A - 8, 2
B - 8, 2, 1
C - 8, 4
D - 8, 4, 1
E - 8, 4, 2
F - 8, 4, 2, 1

To add a flag to a hexadecimal number containing flags, simply add the flag value at that position if it does not exist. In code we use operator |, not +. This is to avoid adding when already exists etc.
Example: add flag 0x100 to 0x402: 0x402|0x100 = 0x502.

To remove a flag from a hexadecimal number containing flags, simply subtract the flag value at that position if it exists. In code we use operator &~ (or just ~ in QM), not -.
Example: remove flag 0x100 from 0x5C0: 0x5C0~0x100 = 0x4C0.

Don't translate a hexadecimal flags number like 0x3011 to decimal, it will not have sense. But can translate a single flag, for example to write shorter, eg can write 4 instead of 0x4, or 16 instead of 0x10.