Using Bit Flags

InstallShield 2019

Project • This information applies to InstallScript projects.

Bit flags are one or more (up to 32) Boolean values stored in a single number variable.

Each bit flag typically has a corresponding predefined constant associated with it; this constant has the bit for this flag set to 1 and all other bits set to 0. For example, the following constant identifies the bit flag for bit 0, that is, the right-most bit in the number:

#define BITFLAG_EXAMPLE_1 0x00000001

 

Other bit flags could be the following:

#define BITFLAG_EXAMPLE_2 0x00000002

#define BITFLAG_EXAMPLE_3 0x00000004

Note that 0x00000003 is not a valid bit flag, since this value corresponds to two bits in the number being set to 1.

Setting Bit Flags in a Variable

To set bit flags in a variable, use the bitwise OR operator (|). For example, to assign the value BITFLAG_EXAMPLE_1 and BITFLAG_EXAMPLE_2 to the number variable nFlags, use code like the following:

nFlags = nFlags | BITFLAG_TEST_1 | BITFLAG_TEST_2;

Clearing a Bit Flag from a Variable

To clear a bit flag from a variable, combine the bitwise AND operator (&) with the bitwise NOT operator (~). For example, to remove the BITFLAG_TEST_1 flag from nFlags, use code such as the following:

nFlags = nFlags & ~BITFLAG_TEST_1;

Testing a Variable for a Bit Flag

To test a variable for a bit flag, use the bitwise AND operator (&) and the bit flag constant to zero all the bits other than the bit for which you are testing. For example, if nFlags is currently set to BITFLAG_TEST_1 | BITFLAG_TEST_2, the following expression evaluates to true (that is, a non-zero result):

nFlags & BITFLAG_TEST_1

All bits other than the rightmost bit are set to 0 by the & operation; the rightmost bit is 1 since it is 1 in both the constant BITFLAG_TEST_1 and nFlags.

The following expression evaluates to FALSE since the third bit of nFlags is 0 and all other bits are set to 0 by the & operation:

nFlags & BITFLAG_TEST_3