A bitmask is a special number that has a bunch of bits (0/1) set to 1 such that when you do a bitwise AND with another number, you get only those bit values. This is useful in many contexts, it basically extracts only the parts of a number that are of interest for a particular task.

One example where a bitmask is used is for subnet masks on IPv4 addresses. An IPv4 address is a 32bit integer value that represents the address of a device on the internet (computer, router, server, etc..). It is usually written in the form where each number is one byte (8 bits) which is a number between 0 and 255. So, the four numbers together make up the 4 bytes (32bits) address. A typical "local network" subnet mask is which is a number which has all leading 24bits set to 1. If you do a bitwise AND between the mask ( and the address, you get And if you do it with the address, you also get You see how this can be useful to determine if the address is part of the particular local network or not.

Another example of where bitmasks are useful is when you use different bits of a single number to mean different things, which are usually called "flags". It's a basic mechanism to store a bunch of simple options within a single number (if you have enough bits to accommodate it). Here is an example of that:

// Example of a simple "paint brush" options:

// first three bits are for color:
#define COLOR_MASK    0x07

#define COLOR_WHITE   0x07
#define COLOR_BLACK   0x00
#define COLOR_RED     0x01
#define COLOR_GREEN   0x02
#define COLOR_BLUE    0x04

// next bit is for shape:
#define SHAPE_MASK    0x08

#define SHAPE_SQUARE  0x00
#define SHAPE_ROUND   0x08

// next 4 bits are for the size:
#define SIZE_MASK     0xF0

#define SIZE_NORMAL   0x00
#define SIZE_SMALL    0x10
#define SIZE_TINY     0x20
#define SIZE_LARGE    0x80
#define SIZE_HUGE     0x90

With that, you can define a paint-brush configuration like this:

int paintbrush_config = COLOR_RED | SHAPE_ROUND | SIZE_LARGE;

And then, the config can be tested using the masks as so:

switch( paintbrush_config & COLOR_MASK ) {
    // do code for painting in white..
  case COLOR_RED:
    // do code for painting in red..
  // ... so on..

switch( paintbrush_config & SIZE_MASK ) {
    // do code for painting a normal stroke..
  case SIZE_LARGE:
    // do code for large ...
  // .. so on ..

You see, this is a simple (low-level) mechanism to extract only selective options by doing a bitwise AND (which is the & operator in C/C++) with the mask. This is used extensively in many APIs and low-level coding (e.g., hardware registers are often options that are stored as a sequence of bits that are read and written using masks and the bitwise AND and OR operations, which are used a lot in hardware drivers and kernel code).

Basically, whenever you see functions that you can set a number of flags using the OR syntax like I showed (COLOR_RED | SHAPE_ROUND | SIZE_LARGE), it means that those flags are probably read using a bitmask in a similar way as I showed with the switch statements above.

This article has been dead for over six months. Start a new discussion instead.