{ifbit:bitset:bit:text:else_text}

Description

Tests whether bits are set in an integer bitset (a number whose individual bits are independent on/off flags). Reads option-text pairs after the bitset: for each pair, the option is a bitmask and the text is returned when every bit of that mask is set in the bitset - that is, when bitset bitwise-AND mask equals mask. The first matching pair wins; if none match, the trailing else text is returned. A mask of 0, or an empty mask, always matches (useful as a default branch). Inside the returned text, _#1 expands to the bitset value and _#2 to the matched mask. It is the bitmask counterpart of the ifeq and ifin conditionals, but reads several flag conditions in one call.

Parameters

bitset required

The integer to test, treated as a set of bit flags. Non-numeric input is cast to an integer (so an empty or non-numeric bitset counts as 0). Usually a field getter such as a flags or status field.

bit required

A bitmask to test against the bitset. The paired text is returned only when EVERY bit of this mask is set in the bitset (bitset bitwise-AND mask equals mask), so a multi-bit mask like 12 means bit-4 AND bit-8. A mask of 0, or an empty mask, always matches. You may repeat bit:text pairs to test several masks; the first that matches wins.

text required

Output returned when its paired bit mask matches. May contain any AA expression; the alias _#1 expands to the bitset value and _#2 to the matched mask.

else_text optional

The trailing odd parameter, returned when no bit:text pair matched. Optional; omitting it yields an empty string when nothing matches. The _#1 alias (bitset value) still expands here, but _#2 is empty because no mask matched.

Examples

test{ifbit:13:8:set:unset}
Expectedset
Actualset
13 in binary is 1101, so bit-8 is set; the bitset bitwise-AND 8 equals 8, so the first text wins.
test{ifbit:13:2:set:unset}
Expectedunset
Actualunset
Bit-2 is not part of 13 (1101), so 13 bitwise-AND 2 is 0, not 2; no mask matches and the else text is returned.
test{ifbit:13:12:both 4 and 8:no}
Expectedboth 4 and 8
Actualboth 4 and 8
The mask 12 is bit-4 plus bit-8. The text returns only when BOTH are set. 13 (1101) has both, so it matches.
test[{ifbit:5:2:has bit 2}]
Expected[]
Actual[]
5 (101) does not contain bit-2, and no else text is supplied, so the result is empty. Brackets in this example just make the empty output visible.
test{ifbit:13:31:all five:15:low four:12:bits 4+8:none}
Expectedbits 4+8
Actualbits 4+8
Masks are tried in order. 13 is not all of 31, nor all of 15, but it does contain all of 12 (bit-4 plus bit-8), so the third text wins. This is the multi-condition form.
test{ifbit:13:12:_#2 is set in _#1:no}
Expected12 is set in 13
Actual12 is set in 13
Inside the chosen text, _#1 expands to the bitset value (13) and _#2 to the matched mask (12).
test{ifbit:13:0:default branch:no}
Expecteddefault branch
Actualdefault branch
Any value bitwise-AND 0 is 0, which equals the mask 0, so a 0 (or empty) mask always matches. Place it last as a catch-all default.
test{ifbit:13:16:has 16:none of them}
Expectednone of them
Actualnone of them
Bit-16 is not set in 13, so no pair matches and the trailing else text is returned.