Search

3.8a — Bit flags and bit masks

Note: This is a tough lesson. If you find yourself stuck, you can safely skip this lesson and come back to it later.

Bit flags

The smallest addressable unit of memory is a byte. Since all variables need to have unique addresses, this means variables must be at least one byte in size.

For most variable types, this is fine. However, for boolean values, this is a bit wasteful. Boolean types only have two states: true (1), or false (0). This only requires one bit to store. However, if a variable must be at least a byte, and a byte is typically 8 bits, that means a boolean is using 1 bit and leaving the other 7 unused.

In the majority of cases, this is fine -- we’re usually not so hard-up for memory that we need to care about 7 wasted bits. However, in some storage-intensive cases, it can be useful to “pack” 8 individual boolean values into a single byte for storage efficiency purposes. This is done by using the bitwise operators to set, clear, and query individual bits in a byte, treating each as a separate boolean value. These individual bits are called bit flags.

Defining bit flags in C++14

In order to work with individual bits, we need to have a way to identify the individual bits within a byte, so we can manipulate those bits. This is typically done by defining a symbolic constant to give a meaningful name to the bit. The symbolic constant is given a value that represents that bit.

Because C++14 supports binary literals, this is easiest in C++14:

Now we have a set of symbolic constants that represents each bit position. We can use these to manipulate the bits (which we’ll show how to do in just a moment).

Defining bit flags in C++11 or earlier

Because C++11 doesn’t support binary literals, we have to use other methods to set the symbolic constants. There are two good methods for doing this. Less comprehensible, but more common, is to use hexadecimal. If you need a refresher on hexadecimal, please revisit lesson 2.8 -- Literals.

This can be a little hard to read. One way to make it easier is to use the left-shift operator to shift a bit into the proper location:

Using bit flags to manipulate bits

Now that we’ve defined a set of bit flags, we can use them to check the value of individual bits, or turn bits on and off.

To query a bit state, we use bitwise AND:

To set a bit (turn on), we use bitwise OR:

To clear a bit (turn off), we use bitwise AND with an inverse bit pattern:

To toggle a bit state, we use bitwise XOR:

Bit flags in real life

In OpenGL (a 3d graphics library), some functions take one or more bit flags as a parameter:

GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT are defined as follows (in gl2.h):

Here’s a less abstract example for a game we might write:

Why are bit flags useful?

Astute readers will note that the above myflags example actually doesn’t save any memory. 8 booleans would normally take 8 bytes. But the above example uses 9 bytes (8 bytes to define the bit flag options, and 1 bytes for the bit flag)! So why would you actually want to use bit flags?

Bit flags are typically used in two cases:

1) When you have many sets of identical bitflags.

Instead of a single myflags variable, consider the case where you have two myflags variables: myflags1 and myflags2, each of which can store 8 options. If you defined these as two separate sets of booleans, you’d need 16 booleans, and thus 16 bytes. However, using bit flags, the memory cost is only 10 (8 bytes to define the options, and 1 byte for each myflags variable). With 100 myflag variables, your memory cost would be 108 bytes instead of 800. The more identical variables you need, the more substantial your memory savings.

Let’s take a look at a more concrete example. Imagine you’re creating a game where there are monsters for the player to fight. When a monster is created, it may be resistant to certain types of attacks (chosen at random). The different type of attacks in the game are: poison, lightning, fire, cold, theft, acid, paralysis, and blindness.

In order to track which types of attacks the monster is resistant to, we can use one boolean value per resistance (per monster). That’s 8 booleans per monster.

With 100 monsters, that would take 800 boolean variables, using 800 bytes of memory.

However, using bit flags:

Using bit flags, we only need one byte to store the resistances for a single monster, plus a one-time setup fee of 8 bytes for the options.

With 100 monsters, that would take 108 bytes total, or approximately 8 times less memory.

For most programs, the amount of memory using bit flags saved is not worth the added complexity. But in programs where there are tens of thousands or even millions of similar objects, using bit flags can reduce memory use substantially. It’s a useful optimization to have in your toolkit if you need it.

2) Imagine you had a function that could take any combination of 32 different options. One way to write that function would be to use 32 individual boolean parameters:

Hopefully you’d give your parameters more descriptive names, but the point here is to show you how obnoxiously long the parameter list is.

Then when you wanted to call the function with options 10 and 32 set to true, you’d have to do so like this:

This is ridiculously difficult to read (is that option 9, 10, or 11 that’s set to true?), and also means you have to remember which parameters corresponds to which option (is setting the edit flag the 9th, 10th, or 11th parameter?) It may also not be very performant, as every function call has to copy 32 booleans from the caller to the function.

Instead, if you defined the function using bit flags like this:

Then you could use bit flags to pass in only the options you wanted:

Not only is this much more readable, it’s likely to be more performant as well, since it only involves 2 operations (one bitwise OR and one parameter copy).

This is one of the reasons OpenGL opted to use bitflag parameters instead of many consecutive booleans.

Also, if you have unused bit flags and need to add options later, you can just define the bit flag. There’s no need to change the function prototype, which is good for backwards compatibility.

An introduction to std::bitset

All of this bit flipping is exhausting, isn’t it? Fortunately, the C++ standard library comes with functionality called std::bitset that helps us manage bit flags.

To create a std::bitset, you need to include the bitset header, and then define a std::bitset variable indicating how many bits are needed. The number of bits must be a compile time constant.

If desired, the bitset can be initialized with an initial set of values:

Note that our initialization value is interpreted as binary. Since we pass in the value 3, the std::bitset will start with the binary value for 3 (0000 0011).

std::bitset provides 4 key functions:

  • test() allows us to query whether a bit is a 0 or 1
  • set() allows us to turn a bit on (this will do nothing if the bit is already on)
  • reset() allows us to turn a bit off (this will do nothing if the bit is already off)
  • flip() allows us to flip a bit from a 0 to a 1 or vice versa

Each of these functions takes a bit-position parameter indicating which bit should be operated on. The position of the rightmost bit is 0, increasing with each successive bit to the left. Giving descriptive names to the bit indices can be useful here (either by assigning them to const variables, or using enums, which we’ll introduce in the next chapter).

This prints:

Bit 4 has value: 1
Bit 5 has value: 0
All the bits: 00010010

Note that sending the bitset variable to std::cout prints the value of all the bits in the bitset.

Remember that the initialization value for a bitset is treated as binary, whereas the bitset functions use bit positions!

std::bitset also supports the standard bit operators (operator|, operator&, and operator^), so you can still use those if you wish (they can be useful when setting or querying multiple bits at once).

We recommend using std::bitset instead of doing all the bit operations manually, as bitset is more convenient and less error prone.

(h/t to reader “Mr. D”)

Bit masks

The principles for bit flags can be extended to turn on, turn off, toggle, or query multiple bits at once, in a bit single operation. When we bundle individual bits together for the purpose of modifying them as a group, this is called a bit mask.

Let’s take a look at a sample program using bit masks. In the following program, we ask the user to enter a number. We then use a bit mask to keep only the low 4 bits, which we print the value of.

Enter an integer: 151
The 4 low bits have value: 7

151 is 1001 0111 in binary. lowMask is 0000 1111 in 8-bit binary. 1001 0111 & 0000 1111 = 0000 0111, which is 7 decimal.

Although this example is pretty contrived, the important thing to note is that we modified multiple bits in one operation!

An RGBA color example

Now lets take a look at a more complicated example.

Color display devices such as TVs and monitors are composed of millions of pixels, each of which can display a dot of color. The dot of color is composed from three beams of light: one red, one green, and one blue (RGB). By varying the intensity of the colors, any color on the color spectrum can be made. Typically, the amount of R, G, and B for a given pixel is represented by an 8-bit unsigned integer. For example, a red pixel would have R=255, G=0, B=0. A purple pixel would have R=255, G=0, B=255. A medium-grey pixel would have R=127, G=127, B=127.

When assigning color values to a pixel, in addition to R, G, and B, a 4th value called A is often used. “A” stands for “alpha”, and it controls how transparent the color is. If A=0, the color is fully transparent. If A=255, the color is opaque.

R, G, B, and A are normally stored as a single 32-bit integer, with 8 bits used for each component:

32-bit RGBA value
bits 31-24 bits 23-16 bits 15-8 bits 7-0
RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA
red green blue alpha

The following program asks the user to enter a 32-bit hexadecimal value, and then extracts the 8-bit color values for R, G, B, and A.

This produces the output:

Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300
Your color contains:
255 of 255 red
127 of 255 green
51 of 255 blue
0 of 255 alpha

In the above program, we use a bitwise AND to query the set of 8 bits we’re interested in, and then we right shift them to move them to the range of 0-255 for storage and printing.

Note: RGBA is sometimes stored as ARGB instead, with the alpha channel being stored in the most significant byte rather than the least significant.

Summary

Summarizing how to set, clear, toggle, and query bit flags:

To query bit states, we use bitwise AND:

To set bits (turn on), we use bitwise OR:

To clear bits (turn off), we use bitwise AND with an inverse bit pattern:

To toggle bit states, we use bitwise XOR:

Quiz

1) Given the following program:

1a) Write a line of code to set the article as viewed.
1b) Write a line of code to check if the article was deleted.
1c) Write a line of code to clear the article as a favorite.
1d) Extra credit: why are the following two lines identical?

Quiz answers

1a) Show Solution

1b) Show Solution

1c) Show Solution

1d) Show Solution

3.x -- Chapter 3 comprehensive quiz
Index
3.8 -- Bitwise operators

168 comments to 3.8a — Bit flags and bit masks

  • Vishal

    I did not understand how if(option4 & myflag) would be a query. I assume the above expression evaluated to true. Can anyone explain me how?

    • Alex

      This does a bitwise AND of option4 and myflag. If bit option4 in myflag is 0, the if statement won’t execute. If it’s 1, it will.

      • Vishal

        Thanks for the reply. But I still have some problems because of lack of bit level programming.Is n’t myflag value 00000000 and hence AND operation of myflag with anything would always evaluate to false (0).

        • Alex

          If myflag is 0, then yes, the result will always be false.

          But what if myflag had bit pattern 0110 1000? Then myflag & option 4 would be:

          0110 1000
          0000 1000

          If you AND those together, you get 0000 10000, which evaluates to boolean true.

  • Liam

    In the code mentioned on this page you seem to be using ~ as not, whereas in previous pages you have used ! . What is the difference between the two? Did I miss this somewhere?

    • Alex

      Logical NOT (!) is typically used for boolean values. It will flip a 0 to a 1, or a non-zero number to a 0.
      Bitwise NOT (~) is typically used for bit flags and bit masks. It will flip each individual bit from 0 to 1 or 1 to 0.

  • Rich

    That ‘bit wasteful’ joke is a groaner! But I laughed 🙂

  • Jan

    Hey Alex,

    firstoff thank you so much for your great tutorials.

    Second, I think in the code above the comments should not state that it is hex. The variables/bitflags are initialized with regular decimal integer literals. There is no 0x before the literal.

  • jack

    is it ok Alex to use decimal values instead of hex?

  • Angel

    Hello Alex, I am quite confused about the following code:

    Why do we need to use the left-shift operator here?
    Isn’t "0x1" already the hexadecimal for 0000 0001, instead of "1 << 0x1"?

  • Michael

    A nice tutorial indeed!And by the way do you teach networking and graphics in c++?

    • Alex

      No. Graphics and Networking aren’t part of the C++ core, so they are outside the scope of these tutorials, which are focused on the core language and standard library.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter