Example: Flags and bitmasks

The bitwise logical operators are often used to create, manipulate, and read sequences of flags, which are like binary variables. Variables could be used instead of these sequences, but binary flags take much less memory (by a factor of 32).

Suppose there are 4 flags:

flag A: we have an ant problem

flag B: we own a bat

flag C: we own a cat

flag D: we own a duck

These flags are represented by a sequence of bits: DCBA. When a flag is set, it has a value of 1. When a flag is cleared, it has a value of 0. Suppose a variable flags has the binary value 0101:

1

varflags = 0x5; // binary 0101

This value indicates:

flag A is true (we have an ant problem);

flag B is false (we don’t own a bat);

flag C is true (we own a cat);

flag D is false (we don’t own a duck);

Since bitwise operators are 32-bit, 0101 is actually 00000000000000000000000000000101, but the preceding zeroes can be neglected since they contain no meaningful information.

A bitmask is a sequence of bits that can manipulate and/or read flags. Typically, a “primitive” bitmask for each flag is defined:

1

2

3

4

varFLAG_A = 0x1; // 0001

varFLAG_B = 0x2; // 0010

varFLAG_C = 0x4; // 0100

varFLAG_D = 0x8; // 1000

New bitmasks can be created by using the bitwise logical operators on these primitive bitmasks. For example, the bitmask 1011 can be created by ORing FLAG_A, FLAG_B, and FLAG_D:

1

varmask = FLAG_A | FLAG_B | FLAG_D; // 0001 | 0010 | 1000 => 1011

Individual flag values can be extracted by ANDing them with a bitmask, where each bit with the value of one will “extract” the corresponding flag. The bitmask masks out the non-relevant flags by ANDing with zeros (hence the term “bitmask”). For example, the bitmask 0100 can be used to see if flag C is set:

1

2

3

4

// if we own a cat

if(flags & FLAG_C) { // 0101 & 0100 => 0100 => true

// do stuff

}

A bitmask with multiple set flags acts like an “either/or”. For example, the following two are equivalent:

Flags can be set by ORing them with a bitmask, where each bit with the value one will set the corresponding flag, if that flag isn’t already set. For example, the bitmask 1100 can be used to set flags C and D:

1

2

3

// yes, we own a cat and a duck

varmask = FLAG_C | FLAG_D; // 0100 | 1000 => 1100

flags |= mask; // 0101 | 1100 => 1101

Flags can be cleared by ANDing them with a bitmask, where each bit with the value zero will clear the corresponding flag, if it isn’t already cleared. This bitmask can be created by NOTing primitive bitmasks. For example, the bitmask 1010 can be used to clear flags A and C:

1

2

3

// no, we don't neither have an ant problem nor own a cat

varmask = ~(FLAG_A | FLAG_C); // ~0101 => 1010

flags &= mask; // 1101 & 1010 => 1000

The mask could also have been created with ~FLAG_A & ~FLAG_C (De Morgan’s law):

1

2

3

// no, we don't have an ant problem, and we don't own a cat

varmask = ~FLAG_A & ~FLAG_C;

flags &= mask; // 1101 & 1010 => 1000

Flags can be toggled by XORing them with a bitmask, where each bit with the value one will toggle the corresponding flag. For example, the bitmask 0110 can be used to toggle flags B and C:

1

2

3

4

// if we didn't have a bat, we have one now, and if we did have one, bye-bye bat

For didactic purpose only (since there is the Number.toString(2) method), we show how it is possible to modify the arrayFromMask algorithm in order to create astring containing the binary representation of a number, rather than an array of booleans: