# 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.

Bit counting

When talking about individual bits, we typically count from right to left, starting with 0 (not 1). So given the bit pattern 0000 0111, bits 0 through 2 are 1, and bits 3 through 7 are 0.

So although we are typically used to counting starting with 1, in this lesson we’ll generally count starting from 0.

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 (turn them on and off). This is typically done by defining a symbolic constant to give a meaningful name to each bit used. 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

The next thing we need is a variable that we want to manipulate. Typically, we use an unsigned integer of the appropriate size (8 bits, 16 bits, 32 bits, etc… depending on how many options we have).

Variable myflags, defined above, will hold the actual bits that we’ll turn on and off. How do we turns those bits on and off? We use our bit flags (options).

Turning individual bits on

To set a bit (turn on), we use bitwise OR equals (operator |=):

Let’s unpack this one to show how it works in more detail.

myflags |= option4 is the equivalent of myflags = (myflags | option4).

Evaluating the portion between the parenthesis:

```myflags = 0000 0000 (we initialized this to 0)
option4 = 0001 0000
-------------------
result  = 0001 0000
```

So we get myflags = 0001 0000. In other words, we just turned the 4th bit on.

We can also turn on multiple bits at the same time using bitwise OR:

Turning individual bits off

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

This works similarly to the above. Let’s say myflags was initially set to 0001 1100 (options 2, 3, and 4 turned on).

myflags &= ~option4; is the equivalent of myflags = (myflags & ~option4).

```myflags  = 0001 1100
~option4 = 1110 1111
--------------------
result   = 0000 1100
```

So 0000 1100 gets assigned back to myflags. In other words, we just turned off bit 4 (and left the other bits alone).

We can turn off multiple bits at the same time:

Flipping individual bits

To toggle a bit state, we use bitwise XOR:

Determining if a bit is on or off

To query a bit state, we use bitwise AND:

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.

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
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?

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

• ronald

// byte-size value to hold some combination of the above 8 options
unsigned char myflags = 0; // all options turned off to start

i am so clueless as to what this is supposed to do??

• Programer unknown

// byte-size value to hold some combination of the above 8 options
unsigned char myflags = 0; // all options turned off to start

i am so clueless as to what this variable is supposed to do is it the byte that will hold all the flags like as if the flags where a indivisual bits.

• Alex

Yes.

This is kinda like stock C stuff - in C though, we typically (or at least, very often) handle them this way:

Leaving aside differences in how constants are defined, using the left shift operator to define the bits themselves increases readability somewhat and makes iteration a lot simpler, as you can see.  The example could be extended to the full eight bits, but I'm very tired at this point, runnin' out of juice.

The bits are like 1-bit elements in a byte- (or short/long/whatever) sized array.  1<<0 is the 0th bit (like the 0th element of an array), 1<<1 is the 1st, and so on.

Doing this in the preprocessor means that it's all pre-computed at compile time.  Care should be taken that any C++ version of this is also pre-computed, but that should be easy enough.

• Alex

In C++14, you can actually use binary literals now. I don't do it here because I'm guessing many users still aren't on C++14 compatible compilers. But I'll mention it in the lesson as the better option.

Yeah, my compiler does NOT support the C++14 stuff (gcc 4.4), although it DOES support it's own almost-identical extension (it ONLY lacks the '\'' separator).  Well I also have MSVC 19 ("2015"), which does support it .. so it's a mixed bag for me, at least until I update my main server (really old Debian 6).

I still prefer the (1<<x) notation for bitflags though.  While 0b0000'1000 is clearer than 0x08, especially re: accidentally setting two or more bits at once (0x60 might look like a single flag to a novice for example, when it's really 0xb0110'0000), I feel that (1<<x) clearly conveys that it's a single bit, and gives it an index number, effectively.

It's only real caveat is that you might end up with trouble in say, DOS or embedded environments, where int is 16-bit.. that would trip up 0x00 and 0b0000 as well though.  In either case, adding a L or UL to the constant would solve the problem (1L<<x) 0b1010UL, etc.

It's good though that you're addressing this.  I get blank looks from people when I talk about bitflags these days.  Young punks, get off my lawn!

• AMG

Alex,
1) Would suggest initialization: 'unsigned char myArticleFlags (0x0);'
2) Why copy initialization is preferred over direct and uniform initialization. 'unsigned char option_viewed = 0x01;' instead of 'unsigned char option_viewed {0x01};'

• Alex

1) Done.
2) It's not. When initializing fundamental variables, any of the three initialization methods is fine. I tend to use copy initialization just out of habit.

• JoeyM

Using C++11 would the following code now be

• Hardik

it's output is as follows :-
1
2
4
8
16
32
64
128

Does this signify that the whole byte is occupied instead of alloting each byte to a single char variable(options) ? Have u given each bye to single char option or a single bit has been alloted to each of the char variable?

• Hardik

sorry...i meant that it is alloted a single byte to each of the char variable or a whole byte has been alloted to all of the options as a whole using bit flags ?

• Alex

Variable "me" is a char, which takes a whole byte. The flags also take an entire byte each. In the above code, you're doing a bitwise AND on "me" and the flag, resulting in a new byte with the appropriate bits set, which is then being interpreted as an integer (since chars are integers)

• Hardik

i understood that.Thank You. BUT, Then, How does bit flags save memory ?
Plz Explain it to Me ! i always get stuck in this chapter....!!!

• Alex

If you only have one set of states you're tracking, it doesn't save any memory. In the example above, we end up using 9 bytes (8 bytes to define the bit flags and 1 byte to store them), as opposed to using 8 bytes if we just used a single boolean to store each state.

However, if you have multiple objects that you need to store states for, then bit flags start yielding benefits. Let's say you want to store a set of states for both yourself and your evil twin. Using booleans, you'd need 2 full sets of booleans (2 x 8 booleans), which would be 16 bytes. However, with bit flags, it would only cost 10 bytes (8 bytes to define the bit flags, and 1 byte per object to store the bits). So we're already seeing savings. Those savings continue to increase with additional objects.

• Hardik kalra

But How? Without using bit flags also it would end up using 10 bytes....Cant The Second variable(me1) access the same options as it was accessed by the first variable(me) ?

And One More Help From You.....
Plz Define A Set Of Options(Like Above) But, That shouldnt Use Bitflags

• Alex

With bit flags, 2 variables (total cost = 10 bytes):

Without bit flags (total cost = 16 bytes):

• Hardik kalra

Thats What I'm asking.....Can't The Second Variable, that is, eviltwinflags access the meisMad Or Any of the options that have been used for "me" !?

If yes, then see, it will also consume 10bytes only

And One More Question !
When i try to print isMad Or Any Option without printing it like (me & isMad), it prints it as an unrecognizable character......Why?

• Alex

> Can’t The Second Variable, that is, eviltwinflags access the meisMad Or Any of the options that have been used for "me" !?

Yes, it can. That's why bit flags are useful -- we only need to define the bit flags once, and then we can use them over and over.

The bit flags are printing as funny characters because they're printing as chars, and chars with values lower than 32 are "unprintable", so your OS is subbing in some other symbol.

• Hardik kalra

Here, in This Code...
false;
bool meIsHappy = false;
bool meIsLaughing = false;
bool meIsAsleep = false;
bool meIsCrying = false;

bool evilTwinIsHungry = false;
bool evilTwinIsHappy = false;
bool evilTwinIsLaughing = false;
bool evilTwinIsAsleep = false;
bool evilTwinIsCrying = false;

Can the eviltwinflags access the options that have been used for "me" !? I meant to ask this !

For Eg. evil twin flags |= meIsMad | meIsHungry; //initial value of evil twin flags was zero

• Alex

> Can the eviltwinflags access the options that have been used for "me" !?

In the bit flags case, yes. In the boolean case, no.

This is fine if meIsMad and meIsHungry are bit flags. This is not fine if they are booleans. The difference is that the bit flags don't actually hold any values -- they're just bit position indicators. So they can be shared. The variable that holds the actual values (for me and my evil twin) needs to be unique, and can't be shared.

• Hardik

Don't you think when we do (myflags & option4) it evaluates to ZERO, so how does it query the bit state, or, here, & is an overloaded operator ?

• Alex

I'm not sure I understand the question. myflags & option4 causes operator & to evaluate operands myflags and option4 to produce a bitwise result. It may evaluate to zero or non-zero depending on the value of myflags.

• Hardik

And I think all the bitwise operators are overloaded when u use them to turn on, off, toggle or query their state !
am I right or not? Plz tell me !

• Alex

No, they're not overloaded in this context.

• Hardik kalra

So If I try to assign boolean values to evil twin flags....will it give an error ?

• Alex

Probably not, since booleans can be converted to chars or other integers. It just won't do what you want.

• Hardik kalra

Thats what i meant to say :-
bool meIsHungry = false;
bool meIsHappy = false;
bool meIsLaughing = false;
bool meIsAsleep = false;
bool meIsCrying = false;

bool evilTwinIsHungry = false;
bool evilTwinIsHappy = false;
bool evilTwinIsLaughing = false;
bool evilTwinIsAsleep = false;
bool evilTwinIsCrying = false;

Inspite of using 2 sets of booleans for two vars we can just use one set for two variables....i aint talking about modifications here

Same memory would be saved
(8bytes + 2bytes) = 10Bytes would be used

• Alex

No. Each boolean represents a specific piece of memory that we're using to store a value. That can't be shared between two variables.

• Hardik kalra

And If We Try To Use Integers !?

• Alex

Doesn't matter. You seem to be missing some fundamental points, so let me try one last time to see if I can make this clear. From the beginning.

In order to store a value (of any type) we need a variable to do so.

If order to store 2 values, we need 2 variables:

We can't use the same variable to hold two (or more) independent values, because as soon as we assign the variable a second value, it will lose its first value (and vice-versa). We need independent memory locations to hold independent values.

Now consider the case where we want to store 8 independent boolean values:

Because these need to be separate values, we have to use 8 boolean variables. If we needed two sets of these, we'd need 16 boolean variables.

This works, but in the case of bool it's a waste of memory. Because machines tend to be byte-addressable, a bool takes 1 byte (8 bits). But only 1 of those bits is actually used to store true or false -- the 7 bits go to waste.

If only there were some way to use those other 7 bits. There is -- bitwise operators. By being clever with the bitwise operators, we can get and set the individual bits in a byte, which allows us to compact 8 unique boolean values into a single byte. For our single byte, we typically use an unsigned char.

But in order to do this, we need to be able to identify each bit individually. We typically say a byte is 8 bits, so we can number the bits from left to right: 8 7 6 5 4 3 2 1. Then all we need to do is say: turn bit 5 on, turn bit 4 off, flip the state of bit 2.

The bit numbers are reusable because they're just bit positions. The unsigned char needs to be unique so we can store a unique set of 8 bits.

Our bit flag variables act as the bit numbers (which is why they are reusable -- they just identify which bit we're talking about, not the value we're trying to get or set). The unsigned char (me or my evil twin) act as the variable that stores a unique set of 8 bits.

If you have any minor clarifications needed about this, I'm happy to address. But if you still don't get it, then I'd say move on. This isn't critical to know and it won't block you from proceeding.

• Hardik kalra

So The Conclusion is that we cannot re-assign fundamental data types to two variables but this is not the case with BitFlags.....Ok !

• Hardik kalra

Sorry I Didnt mean to say this...i know that we cannot use one variable to store two values.....this is basic....what i meant to ask is that :-
for eg :-
bool opt1 = false;
bool opt2 = true;

bool sopt1 = false;
bool sopt2 = true;

//Now Lets Declare two char vars :-
unsigned char myflags = !opt1;
unsigned char eviltwinflags = opt2;

See Here There was no requirement to define sopt1&2....that's what i meant to say that why we are declaring two sets of booleans if our work can be done by just using one set......

Yes I Agree on one point that we can produce an aggregated result using bitwise operators;
For Eg -- myflags |= opt1 | opt2;
(opt1 and opt2 are bit flags here).....

I Hope Now u have understood my question !

• Alex

You would never do this. You're mixing metaphors. The top set of 4 booleans is what you'd do without bit flags, since you're using discrete variables to hold values.

If you're using bit flags, you need two things: a variable to hold the bits (myflags and eviltwinflags) AND the bit positions. We use symbolic constants to give meaning to the bit flags (e.g. isHappy, because that's more meaningful than 0x4).

You'd never use booleans as the bit positions because booleans only hold 2 values (0 and 1), which is 1 bit. If you used booleans as bit flags then you'd have no way to access the other 7 bits in your variable.

• Jim Smith

For those of us for whom English is a foreign language it can be a little confusing the sentence "Write a line of code to unset the article as a favorite."

Searching for the meaning of the verb "to unset". I have found that most reputable online dictionaries don't even recognize this as a verb but only as an adjective with the following meanings:
a) not fixed in a setting, unmounted (e.g. unset diamonds)
b) not firmed or solidified (e.g. unset concrete)

Some sources say that "to unset" means "(label) to make not set" (which is even more confusing). How can I set a bit in such a way as to make it "not set"?

On the other hand, the verb "TO RESET" has a clear meaning in engineering, "to restore (a gauge, dial, etc) to zero". That meaning was borrowed by Computer Science where it means "clear to restore (the contents of a register or similar device) in a computer system to zero".

TLDR unset = reset?

• Alex

In this context, unset = reset. Reset is a bit ambiguous because it's unclear whether you're resetting to 0 or resetting to the initial state, whatever that was. So "unset" is sometimes used to mean "return to the off state".

However, in this context, the word "clear" works just as well, so I've replaced "unset" with "clear", which makes it clear(er) we're setting bits back to 0.

• Dani

#include <bitset>
#include <iostream>

const int option_1 = 0;
const int option_2 = 1;
const int option_3 = 2;
const int option_4 = 3;
const int option_5 = 4;
const int option_6 = 5;
const int option_7 = 6;
const int option_8 = 7;

int main()
{
std::bitset<8> bits(0x2);
bits.set(option_5);
bits.flip(option_6);
bits.reset(option_6);
std::cout << "Bit 4 has value: " << bits.test(option_5) << '\n';
std::cout << "Bit 5 has value: " << bits.test(option_6) << '\n';
std::cout << "All the bits: " << bits << '\n';
std::cout << "size of bits  " <<sizeof(bits) ;// _________shows 4 byte ____________//

return 0;
}

i have a question ,i guess that size of "bits"(source code above) will need 1 byte( 8 bits) only,after i compiled the code it shows 4 byte.can you explain me ?Thanks for nice tutorial

• Alex

This comes down to how std::bitset is implemented, which may differ from compiler to compiler. But most likely, your version of std::bitset is allocating 32 bits (one word) for anything between 1 and 32 bits for efficiency purposes. This makes the code execute faster at the cost of potentially wasting some memory.

• Dani

Thanks a lot for your explanation,
i got it.

• 0phoff

Hey,

Here you write that this code will use 9bytes (8 for options & 1 for the flag itself), but I'm not entirely agreeing with that.
Because you define the options as being const (and you never use address-of), wouldn't that allow the compiler to inline the values?
I know the c++ standard doesn't force compilers to inline const statements, but I think most of them do!
This would mean that, even for situations where you have only a few flags, the way with bits would still take less memory (but more computation time because of the binary operators...)

Let me know if I am right or not! 🙂

• Alex

Maybe I should say, 9 bytes in the worst case. You're right though, a good compiler would probably inline the const variables, making the code slightly bigger but reducing the memory footprint.

• 0phoff

Would the code be bigger though?
Anyway I am just nitpicking..
And this could be solved with enums and then there is no discussion! 😉

• Alex

Good question, actually. I was thinking about inline functions, which tend to make more code than non-inline functions because the code gets copied into many places instead of staying in a single place. But with literals, at least with fundamental types, this probably isn't true (it might be for more complex types, like classes).

• Terry

Hi Alex, how come you use | to specify both options 4 and 5 to turn on, wouldn't it be & because you would be saying option4 AND option 5?

• Alex

No. And in fact, bitwise logic tends to be the opposite of how we use terms in common language. In common language we tend to think of OR as a restriction (you can have "A" or "B", you're restricting the choice from a set" and AND as an aggregation (A and B is more than A or B alone).

In bitwise logic, it's the opposite. OR produces a result that includes all the bits in both operands, so A OR B includes all the bits from both A and B (an aggregation). AND produces a result that includes only the bits in both A or B (a restriction).

This makes sense when you consider things from a bit by bit level, but it does make it a little hard to wrap your head around the first time.

• andy mackay

Just spent half an hour scratching head over the very first example:-

Testing to see if an option is set using bitwise AND
if (myflags & option4)

I was thinking for ages that wouldn't show if option4 was set as it would give 0000 0000, but it just dawned on me that we are testing if myflags has option option4 set and not checking to see if option4 is set, jeez and can be thick sometimes, lol.

• Victor

Hello again, Alex.

Please could you tell us about bit fields - what they are and how to use them? I hope I'm not asking for too much...

• Alex

Bit fields allow you to create members of a struct that use less that 1 byte each. This can be useful if you want to compact multiple sub-byte members together to save space. However, with the amount of memory modern machines tend to have, bit fields aren't used near as much these days, so I haven't written a full lesson on them yet. I've added it to my to-do list for the future.

• Victor

Hi Alex and thanks again for this great site.

If one is using std::bitset, is it possible to use typedefs to name custom types rather than having to type out std::bitset<blah> for every declaration, and if so would you consider this good practice?

Thanks.

P.S.: How do I get my photo to display in comments 🙂 ?

• Alex

Yes, you can use a typedef (or type alias). It's fine to do this if you want.

To get a photo to display in comments, you need to set up a gravatar with your associated email address. See gravatar.com.

• James Ray

Hi Alex,

After your first example, it may be helpful to post a link to the tutorial on literals. I had to go back as I forgot that hexadecimal counts as 0-9, then A-F in one digit, to go up to 15. I went back because I wanted to check how one digit would go past 9.

• James Ray

Oh, I see that you already had a link to section 2.8 -- literals, later on. Maybe it's better to put it straight after when it comes up first in the tutorial after the first example?

• Alex

Good call. I've added a note to the lesson. Thank you.

• vishal

why not just ,me= & isHappy? but me= &~isHappy; I do not understand the purpose of not operator there?

• Alex

Turning a bit on is easy -- we just do a logical OR. Turning a bit off is a little more difficult, since C++ doesn't have a logical operator that maps directly to that behavior. So instead, we have to combine two logical operators to get the desired effect.

Consider the statement me &= ~isHappy. We know isHappy has value 0000 1000, so ~isHappy has value 1111 0111. We don't know what value "me" could have, it could be anything. So let's just say the bits are represented by the letters abcd efgh. Our statement is now abcd efgh &= 1111 0111.

For any given bit x, if you logical AND x with a 1, x will retain its value. If you logical AND x with a 0, x will be turned off. By using logical NOT on isHappy, we produce exactly the right bit pattern to use with logical AND in order to have all of the bits preserve their original values except the one we want to turn off.

• Igna

Typo... you miss the word " a " before "game" in the following statement "Imagine you’re creating game where there are monsters for the player to fight", I think it should be "Imagine you’re creating a game where there are monsters for the player to fight"

And thanks again for the tutorial...

PD: Sorry about my English but I speak Spanish

• Anonymous

Can the 1a, 1b, 1c questions be solved by using bitset ? And are bitwise operators totally replaceable  with bitset ? Because it'll be great if everything can be done using bitset.

• Alex

Yes, quiz questions 1a, 1b, and 1c could be solved that way. Variable myArticleFlags could be a std::bitset and the quiz code should still work (since std::bitset supports bitwise operators).

However, if you're using std::bitset, you'd want to consider defining the option_xxx values differently (since the bitset functions use indices rather than integer values).

std::bitset is great, particularly if you want to tweak single bits. It doesn't do quite as well if you need to query or set multiple bits at once.

• Anonymous

Can we query, set, reset multiple bits at once by using bitset ? If so, how ?

• Alex

You can, by using the &, |, and ^ operators. You can not via the set(), reset(), and flip() member functions.

• Eric

Alex,

You're missing the

at the bottom of the RGBA color example.

Thanks for the great work with these tutorials.

• Alex

Updated, thanks!

• Karlo

Hi, Alex! I have a suggestion when turning off bits:

When you have something like this:

in case we need to modify more options at once, I found it better to use DeMorgan's law, like this:

This way you keep the consistency of listing multiple options with | operator like with other actions, and it's slightly more readable. Maybe add that to the lesson? 🙂

Also, thank you very much for these lessons. Very well organized, easy to follow, and most important, under constant maintenance and open to suggestions. Keep it up! 🙂

• Alex

Love it! That's definitely better than what I had. I've added it to the lesson. Much appreciated.

• nikos-13

"  const int option_1 = 0;
const int option_2 = 1;
const int option_3 = 2;
const int option_4 = 3;
const int option_5 = 4;
const int option_6 = 5;
const int option_7 = 6;
const int option_8 = 7;

int main()
{
std::bitset<8> bits(0x2); // we need 8 bits, start with bit pattern 0000 0010  "

That means that our first option will be option_2 ?????

• Alex

Yes, this will start initialized with option_2.

• nikos-13

Thank you Alex!!!

• nikos-13

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

someFunction(option10 | option32); "

In this case, where we have to define our options? at someFunction or main???

• Alex

Good question. In this case, both the caller and callee are going to need access to the options. Probably the best thing to do would be define them at the top of your file (just below the includes) (as global variables, which I cover at the start of chapter 4) or in a header file (which you can then #include).

• nikos-13

"

// Define 8 separate flags (these can represent whatever you want)
// Note: in C++11, better to use "uint8_t" instead of "unsigned char"
const unsigned char option1 = 0x01; // hex for 0000 0001
const unsigned char option2 = 0x02; // hex for 0000 0010
const unsigned char option3 = 0x04; // hex for 0000 0100
const unsigned char option4 = 0x08; // hex for 0000 1000
const unsigned char option5 = 0x10; // hex for 0001 0000
const unsigned char option6 = 0x20; // hex for 0010 0000
const unsigned char option7 = 0x40; // hex for 0100 0000
const unsigned char option8 = 0x80; // hex for 1000 0000

// byte-size value to hold some combination of the above 8 options
unsigned char myflags = 0; // all flags/options turned off to start

"

Could you please show me how to use this in a complete code??? I mean, i understoud how to query,tern on, etc.... but i didn't understand how to define bit flags....

• Alex

I've updated the lesson with an example.

• nikos-13

Thank you very much Alex!!! So, we don't assign those 8 options, we just define them to use them at the byte-size value right???

• Alex

Right. We define them in such a way that we can use them to isolate single bits within a byte.

• Elruin

In bitset you can also access certain bit by myflags[n], where n is number of bit(from right to left: 0, 1, 2 etc). I think it comes handy in "if" sentences.

• Mike

Presumably there is a danger here if a variable is left un-initialised. For example with the quiz, if

isn't initialised, I run the risk of starting from some random configuration, don't I?

• Alex

Yes, absolutely. Presumably you'd want to either initialize myArticleFlags with a value, or assign a value to it shortly after creation.

I've updated the quiz question to initialize it to 0.

• Ignacio

You are amazing man!!! I hope you can do a tutorial like this, but oriented to graphical user interfaces, thanks a lot for this amazing material!!!

• bert

Just curious, you say:

"As a side note, different machine architectures may store bytes in a different order. RGBA can be stored as ARGB or BGRA depending on the architecture."

What machine architectures would use that byte ordering?  I'm only familiar with big and little endian.  Granted, it's been a while, but those orderings don't strike me as an endian difference.

tx!

• Alex

I think I misspoke -- ARGB is an intentional choice to represent the encoding differently, putting the alpha channel first rather than last. Other permutations (ABGR and BGRA) are the little-endian permutations of RGBA and ARGB.

• Oliver Dawkins

Hi, I am thoroughly enjoying these so far, and I'm learning a lot of the finer points of c++ that I didn't at University. In the OpenGL example of bitfalgs, why is there a 4 used in the definition?
[#define GL_DEPTH_BUFFER_BIT               0x00000100
#define GL_STENCIL_BUFFER_BIT             0x00000400
#define GL_COLOR_BUFFER_BIT               0x00004000]
I'm struggling to figure out how I would manipulate that/ why it is used. I was also under the impression that any positive number is classed as true(1), so I'm stuck there as well and any enlightenment would be appreciated!

• Alex

These are hexadecimal numbers (you can tell by the 0x suffix), not binary. A hexadecimal digit is 4 bits (0 hex = 0000 binary, 4 hex = 0100 binary). 8 hexadecimal digits of 4 bits each = 32 bits = 4 bytes.

• Dawid

When I check size of (sizeof()) the 8-bits bitflag "bits" (from the example above) i get that it takes 4 bytes. The size of int option_1 is 4 bytes too. Does it mean that I should use fixed width integrers?

• Alex

It would make sense to use fixed width integers for the options, but there isn't much that you can do about std::bitset<8> taking 4 bytes.

• Jonathan

Is there a way to check that the solutions to the quiz worked correctly? How could I display the state of all the flags at the end of the commands?

• Alex

> Is there a way to check that the solutions to the quiz worked correctly?

Ideally, you'd want to ensure the solutions work with a range of different inputs, and validate that they produce the expected output. You don't necessarily need to print the state of all the flags, but whether that your set of inputs was converted to the set of expected outputs.

> How could I display the state of all the flags at the end of the commands?

You can use a for loop and logical AND to isolate and print each individual bit. Something like this:

• Luis Alonso