# 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

• TJ

Really can't thank you enough!
This simple tutorial with few examples has done something that no tutorial or book or videoseries has not done before; i finally really understand this topic. It's not a hard topic, but other tutorials often "overexplain" this. Now i can dive more deep into it, so thanks again!

• Claudio

Great lesson! This is the first tutorial I read in this webpage cause I use another language, but this works for me.

• Travis

Alex,
Obviously I'm missing something here...

If I'm turning on the 0 and 3 spot for the bitset, shouldn't I get something like 0000 1001 when I print?
You mentioned that you can initialize your bitset with values and that's what I was trying to accomplish here, but, like I said, I think I'm missing something.

The only way I found to get the results I want is the following:

...I'm prepared for the *facepalm*

• Alex

The fault is probably mine. When you initialize a std::bitset, you don't use bit positions, you use binary values. So your op4 shouldn't have value 3 (which is 0000 0011), it should have value 8 (which is 0000 1000).

However, when you use set, flip, and reset, you use bit positions for that. I'll make that distinction clearer in the lesson.

• David

Hi,
there's a mistake / typo in the section on bit masks:

1001 0111 & 0000 1111 is NOT 0000 0001 but 0000 0111 (which is, in fact 7 in decimal).

• Alex

Correct, and fixed. Thanks for pointing that out.

• J3ANP3T3R

Question :
in "if (myflags & option4) ... // if option4 is set, do something" what if myflags is 1001 1101 ? then to evaluate "if (myflags & option4)" would be

1001 1101 (myflags)
0000 1000 (4)
=
0000 1000

which is true but option4 was set to ON but i just could not get pass the fact that the other bits are also evaluated when we only needed to evaluate bit 4. is there a way to compare the target bit column only ?

• Alex

Remember that memory is only byte-addressible -- this means we can't directly address individual bits. However, what we can do is cleverly use the bitwise operators to achieve the same effect. Note that option4 has all of the bits it doesn't care about set to 0. This is so that when we do a bitwise AND (with the entire byte), those bits are guaranteed to resolve to 0, and won't impact the result. Make sense?

• J3ANP3T3R

Makes perfect sense thanks and that part i got just wondering if there would be a performance issue with having to resolve the zeros with longer binary sets.

• Alex

No, it all gets done in parallel.

• J3ANP3T3R

"unsigned int pixel;
std::cin >> std::hex >> pixel; // std::hex allows us to read in a hex value"

Question : how does C++ know when to convert the input to binary ? or is it binary by default ? pixel is unsinged int and the input is in the form of hex stored to pixel which is unsigned int. i did not see any conversion to binary. is unsigned int a binary type ? thanks 🙂

• Alex

Computer memory is a sequence of binary bits, so everything that goes into memory has to be converted to binary. Most modern CPUs have native handling for converting integers and floating point numbers to binary and back, which makes the conversions very fast.

• Nyap

This is really confusing compared to last lesson D: but i'm going to force myself to take this in before moving to the next lesson

• Nyap

if anybody else is having problems understanding the top section, get a text editor out and go through what each thing does step by step! helped me out a lot

• Alex

What specifically did you find confusing? How can I make this lesson better?

• J3ANP3T3R

that could be the parts with the hex value i think they may need a bit more comment to explain the "how". it encourages the reader to do some digging though so all in all already a better lesson.

• Jane Doe

Ad-Blocker off just for you Alex.

• Peng

Thanks a lot

• Alex

No, because we count bits from 0 to 7, from right to left. So option X is bit X-1. Option 6 is bit 5.

• J

I'd just like to add that [b]std::bitset[/b] plays somewhat nicely with C-style bitfields.  You can use it as an interim to display a bitfield's individual bits, and for reading in individual bits and turning them into an integer.  Unfortunately, its bitwise operator overloads expect both operands to be bitfields, which can cause problems in situations where we can't rely on type promotion.

Bitset does bring added convenience, however, in that you can index each bit as if it were an array.

Overall, it's better to either rely on C-style bitfields (and use bitset if you need I/O to work with individual bits instead of hex values), or rely entirely on bitset and eschew C-style bitfields.  Mixing the two is just opening yourself up to complications.

• Elpidius

Hey Alex, I just found a typo:

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

"logical OR" should say "bitwise OR"

• Alex

Fixed. Thanks for pointing that out.

• Lokesh

I think
“A” standard for “alpha” ...
should be
“A” stands for “alpha” ...

• Alex

Yup, thanks for pointing out that typo. Fixed!

• Lokesh

@Alex, I found something interesting:
In the std::bitset code example you have used '\n' for newline instead of "\n". It executes fine.
But "\n" consists of two characters -- '\'(ASCII code 92) and 'n'(ASCII code 110).
And since a char variable is represented within single quotes(''), using '\n' is technically wrong.
Also, in windows a newline consumes 2 bytes instead of 1(I created a text file with just a newline, it consumes 2 bytes).
But, after looking at the ASCII table, I found the unprintable ASCII character with code 10(LF- line feed/newline).
Then I defined a new char variable with value 10 like so:
[code]
char new_line = 10;
std::cout << "First line." << new_line << "Second_line" << new_line;
[code]
and it compiled and produced the same output as using '\n'.
Thus, my question is: Is the compiler interpreting '\n' or "\n" as LF(ASCII code 10), or is something else going on here?
I understand, this question is far off topic of C++. I was just curious.

• Alex

Yes, the compiler interprets '\n' as ascii code 10 on Windows.

There are other "slash commands" that the compiler interprets listed in lesson 2.7 -- Chars.

• Anonymoose

Generally speaking, a character constant in the form of '\c' is called an escape sequence, and is actually the ASCII or Unicode character represented by that; in this case, the backslash isn't interpreted as a distinct character, but as a "flag" indicating that the following character or set of characters is an escape sequence.  (This is also why the actual backslash character is represented as '\\'.)

Each escape sequence maps to a specific character, with most of them mapping to a character that is either untypeable (such as '\n' for the newline character, ASCII/Unicode 10 (0x0A), as you discovered), or otherwise difficult to represent (such as '\'' for the actual ' character, which would otherwise have to be represented as a raw number instead of a character constant).

• Danny

Hey Alex, I propose that you move this tutorial to another level, probably at the end of procedural programming,just before introducing OOP. Its kinda tough for beginners. It is giving me a hard time trying to capture what is there to get, not knowing where to apply it, difficult syntax....... I don't know what the problem is.

• Alex

Yeah, this is a tough one. I'll add a note indicating that this lesson is safe to skip if you get stuck.

Hi, I was wondering of you could explain to me how the following answer you gave would resolve to true were the option deleted?

if (myArticleFlags & option_deleted)

So, to break that down we have (let's make option_deleted bit 4):

if(???1 ???? & 0001 0000)

Ignoring the if we get a bitwise-AND resulting in:

0001 0000

Surely an if-statement only takes a boolean argument and not a collection of bits?

I am confused

We are left

• Alex

Good question. There's a few things you need to connect here:
1) If statements execute only if the conditional is boolean true
2) Integers are considered 'true' if they are non-zero, and false if they are zero.
3) The integer will be non-zero if any of its bits are set, and zero if none of its bits are set.

Therefore, we can say:

A) If we use an integer as the conditional in an if statement, the if statement will execute if the integer has _any_ non-zero bits.

Now, in our example, we're doing a logical AND of option_deleted (0x80) and whatever value is in myArticleFlags. There are two outcomes:
A) If myArticleFlags has a 0 for bit 4, then ???0 ???? & 0001 0000 = 0000 0000. All zero bits = integer 0 = boolean false = the if statement doesn't execute.
B) If myArticleFlags has a 1 for bit 1, then ???1 ???? & 0001 0000 = 0001 0000. Any non-zero bits = integer >0 = boolean true = the if statement executes.

Make sense?

• Shiva

Alex,

I loved the last two modules! These are the sort of things they never taught us at school (and it seems they don't plan to teach us in college either). Bitwise operations may be outdated, but I feel that these are some of the fine points of a programming language like C++. Are they provided in any other programming language? I haven't seen them anywhere else.

BTW one thing to note: In the std::bitset example you have used the 'enum' datatype, which you haven't discussed yet as it comes only in a later module. In case you haven't done this on purpose, you might want to add a note explaining it in brief so that you don't scare off an unsuspecting reader. 😀

• Alex

Many languages (such as Java and PHP) provide bitwise operators, though they aren't commonly used.

I removed the enum and replaced it with standard variables for now. Thanks for noticing that.

• Nigel

Alex,  In ' Introduction to std::bitset' you have the enum:

enum BitOptions
{
OPTION_1 = 1,
OPTION_2 = 2,
OPTION_3 = 3,
OPTION_4 = 4,
OPTION_5 = 5,
OPTION_6 = 6,
OPTION_7 = 7,
OPTION_8 = 8
};

Shouldn't the option values be 0-7?  Your code doesn't use OPTION_8 but if you do, you get a run time Out-of-Range error.

Thanks for a brilliant set of tutorials!

• Alex

Yes, I messed that up. It should be fixed now. Thanks for noticing!

• Mike

How do you make the pixel example with bitset?
Thanx, amazing tutorials.

• Alex

• Tom

Why do the options for the initial example and for std::bitset refer to different bits? for instance option4:

const unsigned char option4 = 0x08; // hex for 0000 1000

yet

std::bitset<8> bits(0x2); // we need 8 bits, start with bit pattern 0000 0010
bits.set(OPTION_4); // set bit 4 to 1 (now we have 0001 0010)

• Alex

This is due to a difference in the way we're counting. In the top set of options, we're counting from 1 (the rightmost bit is 1). std::bitset counts from 0 (the rightmost bit is 0).

• Te Ka

in example from section
"An introduction to std::bitset".
g++ on CentOS7 spits a lot of errors without it

• Alex

Updated. Thanks!

• Phil

The first line requires the namespace std:: while the other three lines don't. As they are part of the bitset library I thought they would also need the std:: prefix but apparently not. I tried compiling with the prefixes but got an error stating that bits.set, bits.flip  and bits.reset are not part of the namespace "std". Could someone explain why they are not required.

• Alex

std::bitset is a type that is defined inside the std namespace in the bitset header, so it needs the std:: prefix.

bits is a variable name that is defined inside your function, so it doesn't need a prefix since it's not inside a namespace.

As for why you don't need to prefix functions set(), flip(), or reset() with std::, the compiler knows that variable bits is of type std::bitset, so it can infer that functions associated with bits are defined inside the std:: namespace as well.

• Cunni

Huge thanks for these well-crafted tutorials, they have given me the needed traction for c++ programming!

I'm wondering if anyone could give an explanation about what happens if you need more than 8 options:

Would we need an additional byte every time we want this option? I'm not sure I have my head wrapped around what would happen if such situations occur in a parameter list.

• Alex

Good question.

If you need more than 8 options, then you'll need to use a larger type for both your bitset and your bit flags.

For example:

• Dex

Hi Alex,

I want to say thank you for putting this tutorial. I learned a lot in this material.

I would like to ask why is it needed to flip option4 in your sample code to turn option 4 off:

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

As I understand this:

myflags = 0x00 // 00000000
option4 = 0x04 // 00000100

using bitwise AND without flipping option4 will also results in turning option4 off. Is it redundant? This is still bugging me. Thanks in advance.

• Alex

To prove your suggestion doesn't work, consider the following case where myflags isn't inintialized to 0:

myflags = 0x14 // 0001 0100

If we AND this with option4, what happens?

myflags = 0x14 // 0001 0100
option4 = 0x04 // 0000 0100

We get 0000 0100.

This means we not only failed to turn off the 4 bit, we also inadvertently turned off the 16 bit.

So clearly doing AND with option4 doesn't work.

Consider how logical AND works:
If we logical AND bit X with a 1 bit, then bit X keeps its current value regardless of whether bit X is a 1 or 0.
If we logical AND bit X with a 0 bit, then bit X is set to 0 regardless of its initial value.

So, to turn off bit X, we want to make sure that the corresponding bit we're ANDing is set to 0. We also don't want to affect any of the other bits, so all the other bits need to be 1s.

So, given some initial value of myflags:
myflags = 0x55 // 0101 0101

To turn just the 4 bit off, we'd want our AND mask to be 1111 1011 (turn off the 4 bit, leave the others alone).

How do we get the bit pattern for 1111 1011? Easy, we invert option4.

• dex

Hi Alex,

Thanks for the reply. I get your point, now I understand it. Thank you so much.

• Mr D

I was really struggling to get my head around this chapter. Then i found a way to print a decimal value as a binary value, which greatly helped with experimenting with and understanding these bitwise operators.

Here’s the code:

• God_Bless_You!

I never lighted bit6! Why It's up??
What Exactly If(statement calculates?)
1000 1100
& 0010 1100
-----------------
0000 1100

So, If(0000 1100)??? // What does this mean? If (What???)

• Alex

Bit 6 isn't set. 0000 1100 is 12 in binary, and any non-zero number converts to boolean true when used as a conditional. Your output will output if bit 3, 4, OR 6 is set.

If you want to change your output so it prints only when bit 3, 4, AND 6 are set, you could do this:

• God_Bless_You!

Thanks for the hint! Thats what i wanted to do! Only if those bits are on!
I still don't understand how if(XXXX XXXX & YYYY YYYY) is interpreted though... (in the Assembler)
Keep doing great job Alex!

• Hello

Hi Alex, I want to ask, what does

means? (myflag & option4) Bitwise And I get it. What if statement calculates???
Thanks for all! 😉

• Alex

If option4 has been set in myflag, then the statement after the if statement will execute.

I talk about if statements in section 2.6 -- Boolean values.

• programmer, another one

can we also use this method for question 1C:

• Alex

No.

Using ^= will toggle the state. If it was off, it turns it on. If it was on, it turns it off.

works whether the option was previously favorited or not.

• Kanchana

I really enjoyed your tutorial and learnt C++ entirely from this tutorial about an year ago. And now I’m going through it again with updated lessons. thank you for writing such a grate tutorial. I have wrote ‘C’ codes for micro-controllers. Hence I have some bad feeling about these two points.

a) In ‘Bit Flags’ section we are going save 7 bytes of data using 8 bits of the same byte. but ultimately we have used 9 bytes of data instead of using one byte. I would rather use

will this make things worse?

b) In ‘An RGBA color example’ section, I like to write the code as

because I feel that is much safer. is there any downside? and also need some explanation on

‘unsigned int’ bit-wise and with ‘signed int’ literal, expression results an ‘int’ assigned to a ‘unsigned char’ without a cast. 🙁

I would like to know your opinion on those few questions. thanks again for this valuable tutorial.

-Kanchana

• Alex

1) In general it's a good idea to stay away from #define macros where you can (this is covered in section 2.9 -- Symbolic constants and the const keyword.

As you've noted, using bit-flags for a single variable really doesn't save anything. Bit-flags typically make more sense when you have hundreds or thousands of variables.

2) Your method works fine. I do mask and shift, you do shift and mask. The end result is the same, and there's no downside to your method.

Visual Studio doesn't throw a warning for these, but some other compilers might complain that casting the results of an unsigned int to an unsigned char is potentially dangerous. You could address this by using static_case to let the compiler know this is intentional:

• Elpidius

I would have to agree with Kanchana on the notion of memory. You've used 9 bytes instead of 1 byte. Although a const variable shows up in the debugger, using a #define symbolic constant doesn't take up any memory. If OpenGL used #define symbolic constants, then we can see that #define symbolic constants aren't all that bad.

Worst-case scenario, the entire program could be written using const variables (for debugging), then all of the const variables could be replaced with #define symbolic constants (for release).

• Alex

Unless you're pushing the performance limits of your hardware, focusing on memory is probably focusing on the wrong thing. You're better off focusing on readability, debugability, maintainability, and consistency. Those are much more important than saving a few bytes here and there.

OpenGL was written before some modern best practices were established. Just because a major library doesn't something doesn't mean it's a good idea -- only that it's possible.

Using different code for debug vs release is a bad idea, and a key way that unintended bugs can creep into your code.

• Kevin Wiggins

I am confused on turning flags on and off. I am new so bare with me.

correct?

so myflags is said to have option 4 and option 5 turned on? 0001 0100 is the bit representation for both options being on?

also is the color example evaluated like this?:

ff7f 3300
ff00 0000
----------
1100 0000
or
ff00 0000

im confused on how 255 came out of that I know hex ff is 255 but how was that resolved from that?

• evan

mate

this is what ff7f3300 is in binary:
1111 1111 0111 1111 0011 0011 0000 0000

and ff000000 is:
1111 1111 0000 0000 0000 0000 0000 0000

• Alex

> so myflags is said to have option 4 and option 5 turned on? 0001 0100 is the bit representation for both options being on?

0001 0100 is the bit representation for option3 and option5 turned on.

As evan said, ff7f3300 in binary is 1111 1111 0111 1111 0011 0011 0000 0000

So when we do this:

We're taking 1111 1111 0111 1111 0011 0011 0000 0000 and converting it into 1111 1111 0000 0000 0000 0000 0000 0000, then right shifting the bits 24 places, which gives us just 1111 1111. That number is 255 in binary.

• MikeM

Alex, speaking as a guy that has intentionally avoided the concept of bitwise concepts since the dinosaurs roamed I want to say thank you. I get it now. One thing I don't get is why

I know I will feel like a dolt, but that is the one part I am unclear on here.

• Alex

You're right, doing the bitwise AND is redundant here, and could be optimized into your second statement.

I intentionally left the redundancy in because I think it's easier to understand what it's doing.

• J3ANP3T3R

hey could you leave a small section for a review of what & | &= does or leave a link to the chapters explaining these. i forgot what they are i only remembered && || there maybe new comers like me as well thanks 🙂

also im a bit confused :

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

but the example showed different. removing a certain portion of a binary number. how do we manipulate multiple bits at once ?

• Alex

The example does exactly what it says it will do. The user enters a number, let's say 151, which is 1001 0111 in binary. Then we do a single & operation with lowMask (0000 1111 in binary), which filters out all of the high bits at once (manipulating multiple bits). 1001 0111 & 0000 1111 = 0000 0111, which is 7.