Search

O.3 — Bit manipulation with bitwise operators and bit masks

In the previous lesson on bitwise operators (O.2 -- Bitwise operators), we discussed how the various bitwise operators apply logical operators to each bit within the operands. Now that we understand how they function, let’s take a look at how they’re more commonly used.

Bit masks

In order to manipulate individual bits (e.g. turn them on or off), we need some way to identify the specific bits we want to manipulate. Unfortunately, the bitwise operators don’t know how to work with bit positions. Instead they work with bit masks.

A bit mask is a predefined set of bits that is used to select which specific bits will be modified by subsequent operations.

Consider a real-life case where you want to paint a window frame. If you’re not careful, you risk painting not only the window frame, but also the glass itself. You might buy some masking tape and apply it to the glass and any other parts you don’t want painted. Then when you paint, the masking tape blocks the paint from reaching anything you don’t want painted. In the end, only the non-masked parts (the parts you want painted) get painted.

A bit mask essentially performs the same function for bits -- the bit mask blocks the bitwise operators from touching bits we don’t want modified, and allows access to the ones we do want modified.

Let’s first explore how to define some simple bit masks, and then we’ll show you how to use them.

Defining bit masks in C++14

The simplest set of bit masks is to define one bit mask for each bit position. We use 0s to mask out the bits we don’t care about, and 1s to denote the bits we want modified.

Although bit masks can be literals, they’re often defined as symbolic constants so they can be given a meaningful name and easily reused.

Because C++14 supports binary literals, defining these bit masks is easy:

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 masks 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 4.12 -- 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:

Testing a bit (to see if it is on or off)

Now that we have a set of bit masks, we can use these in conjunction with a bit flag variable to manipulate our bit flags.

To determine if a bit is on or off, we use bitwise AND in conjunction with the bit mask for the appropriate bit:

This prints:

bit 0 is on
bit 1 is off

Setting a bit

To set (turn on) a bit, we use bitwise OR equals (operator |=) in conjunction with the bit mask for the appropriate bit:

This prints:

bit 1 is off
bit 1 is on

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

Resetting a bit

To clear a bit (turn off), we use Bitwise AND and Bitwise NOT together:

This prints:

bit 2 is on
bit 2 is off

We can turn off multiple bits at the same time:

Flipping a bit

To toggle a bit state, we use Bitwise XOR:

This prints:

bit 2 is on
bit 2 is off
bit 2 is on

We can flip multiple bits simultaneously:

Bit masks and std::bitset

std::bitset supports the full set of bitwise operators. So even though it’s easier to use the functions (test, set, reset, and flip) to modify individual bits, you can use bitwise operators and bit masks if you want.

Why would you want to? The functions only allow you to modify individual bits. The bitwise operators allow you to modify multiple bits at once.

This prints:

bit 1 is off
bit 2 is on
bit 1 is on
bit 2 is off
bit 1 is on
bit 2 is on
bit 1 is off
bit 2 is off

Making bit masks meaningful

Naming our bit masks “mask1” or “mask2” tells us what bit is being manipulated, but doesn’t give us any indication of what that bit flag is actually being used for.

A best practice is to give your bit masks useful names as a way to document the meaning of your bit flags. Here’s an example from a game we might write:

Here’s the same example implemented using std::bitset:

Two notes here: First, std::bitset doesn’t have a nice function that allows you to query bits using a bit mask. So if you want to use bit masks rather than positional indexes, you’ll have to use Bitwise AND to query bits. Second, we make use of the any() function, which returns true if any bits are set, and false otherwise to see if the bit we queried remains on or off.

When are bit flags most useful?

Astute readers may note that the above examples don’t actually save any memory. 8 booleans would normally take 8 bytes. But the above examples use 9 bytes (8 bytes to define the bit masks, and 1 bytes for the flag variable)!

Bit flags make the most sense when you have many identical flag variables. For example, in the example above, imagine that instead of having one person (me), you had 100. If you used 8 Booleans per person (one for each possible state), you’d use 800 bytes of memory. With bit flags, you’d use 8 bytes for the bit masks, and 100 bytes for the bit flag variables, for a total of 108 bytes of memory -- 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.

There’s another case where bit flags and bit masks can make sense. 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, a well regarded 3d graphic library, opted to use bit flag parameters instead of many consecutive Boolean parameters.

Here’s a sample function call from OpenGL:

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

Bit masks involving multiple bits

Although bit masks often are used to select a single bit, they can also be used to select multiple bits. Lets take a look at a slightly more complicated example where we do this.

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:
ff red
7f green
33 blue
0 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 into an 8-bit value so we can print them back as hex values.

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 bitwise NOT:

To flip bit states, we use bitwise XOR:

Quiz time

Question #1

Given the following program:

a) Write a line of code to set the article as viewed.

Show Solution

b) Write a line of code to check if the article was deleted.

Show Solution

c) Write a line of code to clear the article as a favorite.

Show Solution

1d) Extra credit: why are the following two lines identical?

Show Solution


O.4 -- Converting between binary and decimal
Index
O.2 -- Bitwise operators

260 comments to O.3 — Bit manipulation with bitwise operators and bit masks

  • lesson 3.8 << and >> 3.8a both of them are hard lessons
    i skiped them will check them in future

  • Louis Cloete

    Alex, you write that your lowBits example is a bit contrived, but you can use very similar code to convert a number to hex and print it. Just keep shifting the number >> 4 and mask out the low 4 bits and you construct a hex number from right to left. You can also mask out the top 4 bits of a fixed width int and shift the number << 4 for sizeof(number) times. That would be almost as simple as your example and feel much more like useful code instead of a contrived example

    Anyway, it is just a suggestion and you might want to think about how it will affect the understandability of the example for a first time reader with no prior experience if you change it like this.

  • Bad_at_Coding

    After reading this one for few times, I wanned to check did i understood whats been told here.Never did i knew that asking 3 simple question can be so much time consuming.
    Sorry for bad english.

    • I won't go over everything, here's what I found while looking over your code:

      * Try limiting your lines to 80 characters in length to prevent your code from being wrapped on small displays. Most editors have an option to display a vertical line at a specific character count.
      * Don't assume a specific integer width. Use one of the types introduced in lesson 2.4a.
      * Line 57: @input is unused after this line. Don't modify it.
      * @setDifficulty: The following gets the job done too. Only do this if you know that the difficulty bits are successive.

      * Repetitive code is bad. You'll learn how to avoid it later on.
      * Line 322-325: int & int = int. Make sure you followed lesson 0.11.
      * Line 368: Only use the conditional operator if you need the result.
      * Line 383: There's no reason to use 3 zeroes.

      • Shelton

        Hi,
        I want to know the best practice to test the bit flag. Is there an elegant way to avoid use lots of if and else in the code?

        Thanks!

        • Hi Shelton!

          Most of the code can be reduced after loops and arrays have been taught.
          OP did a lot of unnecessary checks, eg. line 238 to 249 and 328 to 331. He's comparing values again although they didn't change. This can be avoided and save some code and/or improve visibility.
          If you post a specific example with lots of ifs that isn't as long as OP's, I might be able to help you better.

  • Boteomap2

    Error ouput:

    i dont know why missing ')'. Help me....

  • There is no $ operator in C++, you meant &

  • Rai

    I tried creating a small RPG with char
    main.cpp

    defence.h

    issue is that when converting the pHP of 0x100 to an integer with static_cast, it reads it as 0 instead of 100.

    Also wondering how to do the following:
    I tried creating a attack.h with

    With this the player chooses which attack they want and it takes health away from the monsterHP. But how do I make it so that the Monster randomly chooses one of the char from namespace defence when defending and when attacking it randomly chooses a char from namespace attack. And this would correspond to taking away the char value from pHP and also monsterHP when the player attacks.

    Also I tried with a while loop for the player to take turn in attacking and defending until either the monster or player HP reaches from 0x100 to 0. I couldn't get it to work.

    Sorry if this is too much to ask, but I feel like this can be possible with what learnt so far. I was just wondering on how to do this. Any help would be appreciated.

    • Hi Rai!

      > converting the pHP of 0x100 to an integer with static_cast, it reads it as 0 instead of 100
      @pHP never is 0x100, because an unsigned char cannot hold 0x100. The maximum value is 0xFF (or 255). Your compiler should've printed a warning.

      > how do I make it so that the Monster randomly chooses one of the char from namespace
      You can't. A namespace only helps you to organize your code, it doesn't have any functionality. You need to store the attack types in an array (Covered later).

      > Also I tried with a while loop for the player to take turn in attacking and defending
      Please share your code and describe what exactly is supposed to happen and what is actually happening.

      > I feel like this can be possible with what learnt so far
      Loops haven't been covered yet, so no. But as long as this doesn't make you skip lessons there's no problem with pre-reading lessons.

      > Sorry if this is too much to ask
      No whackers. Projects like these keep you going. Learning a new language without using it for something you like is boring.

  • Christina

    I have included the bitset standard header. Successfully compiled the following :

    std::bitset<8> bits(0x2); // we need 8 bits, start with bit pattern 0000 0010
        bits.set(option4); // set bit 4 to 1 (now we have 0001 0010)
        bits.flip(option5); // flip bit 5 (now we have 0011 0010)
        bits.reset(option5); // set bit 5 back to 0 (now we have 0001 0010)

    but while executing this piece of code , i get the unhandled exception error
    The thread 0xd5c has exited with code 0 (0x0).
    Exception thrown at 0x75975EF8 in Project3.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00FDF900.

    • Alex

      That's odd. Works for me both on Visual Studio and an online compiler. Anybody else having issues here?

    • Tested, working. I don't see what would be causing the error. Try debugging your program to see where exactly the exception is being thrown from.

      • Christina

        I debugged the piece of code.   Just after executing this instruction :
        bits.set(option4); // set bit 4 to 1 (now we have 0001 0010).

      • Christina

        I just found out the issue. Instead of using int option4 i had used unsigned char option4.
        I think the bitset functions expect the int data types for the bit opertions.

        Now it works fine for me after changing like this:

        // const unsigned char option4 = 0x10; // hex for 0001 0000
        const int option4 = 4;

        Sorry for the confusion.

        Thanks

  • Silviu

    My question here is:
    When it's testing "if(myflags)" appears on, why ? i know what it means in that condition myflags as long as is different from 0 show this else show this.
    When i test it with let's say 1,2,3 will show off, because all this numbers will convert in binary numbers and than compare it.
    But why when it's tested like this, shows on , i mean i think it's testing all the numbers in myflags, if i'm not wrong .
    Thank you.

  • Liam

    Hello,

    In the tutorial stated that it is better to use bitset over standard bit operations. But in case of using it for passing flags to functions I cannot figure how that would be practically useful? Since in order to pass to a function multiple flags we still have to do all the bit operations manually and we still have to define all the flags using bits, not integers that would reflect positions and could be used with bitset functions.

    • Alex

      Good question. std::bitset does have functions and operators that allow you to deal with bitmasks, but as you correctly note, it's a bit awkward that some functions take bit indices and others use bit values. Personally, I'd probably avoid mixing the two, and stick to either using only positions or only values, as appropriate for my program.

  • sam

    This does not work for me because of the ! outside of the parentheses.

  • Dim Tim

    You could also note that in embedded programming bit manipulation is essential: microcontrollers use SFR's - special function registers to control peripherals, and most of the SFR's are collection of single bits or groups of bits, performing actions or reflecting status of the peripheral.
    BTW, here bit fields in records are also very useful.

    P.S.: Thanks for very clear lessons!

  • Will

    Hi, I wanted to ask a question about the RGBA code.
    Why use char for

    when you can replace char with int, removing the use of the static cast function in

    .

    Is there some reason why you would use char over int?
    Is it because each of the RGBA values are 8 bits (1 byte) each and you are trying to save memory?

  • Inan

    Why using a hex valuse while usin decimel also giving same result?

    instead,

  • Anandkumar

    Hi Alex,

    In most of the programs const keyword is used, same needs to be changed to constexpr.
    Rule: Any variable that should not change values after initialization and whose initializer is known at compile-time should be declared as constexpr.

    • Alex

      Most of these tutorials were written before constexpr existed. It's not worth the time to go back and constexpr them. I'll make a note of such on the constexpr tutorial.

      • nascardriver

        If you want me to I can help updating the lessons to use your rules, especially uniform initialization as that's something I point out on the majority on code submissions.

  • J Gahr

    Using std::bitset, I tried to initialize my variable like this:

    I expected it to print 0010 0001 (decimal 33), but it instead printed 0000 0101 (decimal 5). Where did I go wrong?

    • nascardriver

      Hi J!

      The number in your option's names doesn't match their value.
      option1 is 0, not 1.
      option2 is 1, not 2.
      And so on.

      • J Gahr

        Okay, so when I replaced this:

        with this:

        I got the result of 33 (00100001) that I was expecting. I was confused by Alex's std::bitset examples.

    • Rohit

      when you initially had the options equal to 0,1...7, that was in integer form. you need to have them equal binary/hex values. so:

      because for option8, you want the bit flag 1000'0000, instead, when you say option8=7, you have 0000'0111, because that's what decimal 7 is in binary form.

      also, as nascardriver said, you might want to change the variable names to option0,option1...option7 just for consistency

  • Anderson

    How this turns an option on?
    myflags |= option4
    If all options on myflags are off and option4  is off the bits should be:
    0000 0000
    0000 0000
    So OR should also be 0, right? What am I missing?

    • nascardriver

      Hi Anderson!

      option4 isn't off, option4 is 4.

      • Anderson

        OOOH, the options are only used to data manipulation?
        Neat! I was thinking the options were storing the information thenselves :facepalm:

        • Alex

          Yes, the options are just bit manipulators. I'll make this clearer in the lesson, because it may not be obvious if you're not already familiar with the concept. :)

          • Anderson

            Explain about how they are just manipulators before you show how to create then, I got stuck there and didn't even read the rest... If I had read the whole page first I would have understood! Great tutorial btw :)
            Edit: Put this example there, just to demonstrate the use of option:
            myflags |= option4;
            What it actually does is:
            myflags = 0000 0000
            option4 = 0001 0000
            OR result: 0001 0000
            So you effectively turned that on.

            • nascardriver

              You seem to have some trouble with the binary system, a decimal 4 is 0100, not 00010000. 00010000 is 16.

              Here's an example of how conversion between binary and decimal works.
              Let's say we have the binary number 01010001.
              The least significant (last) bit is decimal 1. The next bit is 2, then 4, then 8. Always multiply the previous value by 2 to get the next.
              Write down those decimals values and write the binary number beneath.

              Whenever there's a 1 in the binary number you take to decimal above and sum it up.
              1 + 16 + 64 = 81
              So binary 01010001 is decimal 81.

              • Rohit

                isn't it true that when using bit flags, his option4 would indeed indicate that the 4th bit is turned on? similarly, wouldn't option5 for a bit flag not be 0000 0101, but in fact 0010 0000?

                in this case, you wouldn't want to OR

                but instead you would want

                in order to turn on the fifth bit, correct? even though 0010 0000 is 32 in decimal. This is how i understood it, anyway

                • nascardriver

                  No. If option5 is a decimal 5 then it's a binary 0101. If you want a number with only the fifth bit set you can use

            • Alex

              Good idea. I've tweaked the lesson to do as you suggest. Thanks!

  • yogesh brar

    In this please explain a bit more about

    What does bits(0x2) do, is it just to initialize the byte? like 0000 0010, if we write

    This will initialize the byte as 0000 0001, so this is to initialize I am guessing as I practiced? b'coz I am confused with the 'start' thing which is said in comment.

    Another thing is

    This is setting the 5th bit as you see but we are saying it as the 4th bit? Please clarify.
    Thanks

    • nascardriver

      Hi yogesh!

      "What does bits(0x2) do, is it just to initialize the byte?"
      Yes. 0x02 isn't the index here, but the hexadecimal value. 0x02 will set the bit at index 1.

      "This is setting the 5th bit as you see but we are saying it as the 4th bit?"
      The bit indexes start with 0 at the end of the string representation of the bitset.

      • yogesh brar

        Thanks. Got it in the first place, I think I am asking other thing:
        Let's say -

        Let me explain what I am getting from the 1st line of code till the last line I have written above; if you run this code it sets the byte size of 8 bits which we want to use which is binary 1(0000 0001)[using bits(0x1), then we set (means turn on) the 4th bit(option5) to 1 from 0.

        3rd Line of Code prints all the bits like:

        4th Line of Code says:

        If you see the output here it says: Bit 4 has a value, but if we count the bits from the right it is 5th bit in the output module. This is my doubt with full explanation. Do rectify me where I am wrong; I know I am not right most of the time. But just wanted to ask.
        Thanks

        • nascardriver

          "count the bits from the left"
          You count bits starting from the right, not left.

          • yogesh brar

            I think we count the bits from left.
            Counting the bits from right is what it is giving me the 5th bit. Right?

            • nascardriver

              0001
              Bit at index 0 is set

              0010
              Bit at index 1 is set

              0100
              Bit at index 2 is set

              1000
              Bit at index 3 is set

              • yogesh brar

                Okay. If you checked the output of the

                it is:

                See the fourth bit is not set. It is the 5th bit from the right and 4th from the left which is set.
                I just set the 2nd bit using:

                It was 2nd bit from the right which I am counting but 4th bit is a bit confusing, but I will try to give every shot to check it on my end to learn more about it.
                Thanks.

                • nascardriver

                  I think you're being confused by the option names,
                  option5 is bit index 4
                  option2 is bit index 1

                • Alex

                  In light of this conversation, I've:
                  * Added a subsection talking about how we count bits
                  * Changed the option names from option1 through option8 to option0 through option7. This is slightly less intuitive for naming options, but aligns the option names with the bit numbers, which is probably an overall win.

  • RryanT

    Questions about coding that I've done:

    Thx for these tutorials, its helping me a lot

  • Will

    So if I need 32 options as the example, I would have to do like this std::bitset<32> bits; ?
    And then add all the options from 1 to 32?

    • nascardriver

      Hi Will!
      You're right. If you actually need 32 options then you'll need to declare 32 options.
      Mind sharing what you need that many options for? There might be more convenient ways to handle them than a bitset.

      • Will

        Nothing really, I was just confused due to the 8 bit thing, I thought this was the limit and then I'd need to go for another of 8, etc till the 32.
        Thanks for the info :)

  • Gavin

    I'm trying to program a text based version of Yahtzee for practice. It isn't finished, but my goal is to have it reroll dice if they're considered "Not held". However, when I tried to use this:

    it gave me an error message saying that it expected the parenthesis first. Can you tell me what I'm doing wrong? The code is below.

    • nascardriver

      Hi Gavin!
      The syntax of an if-statement is as follows

      where 'x' is a boolean expression.
      Your exclamation marks are in between the 'if' and the '(', where there's not allowed to be anything but whitespace. The correct place to apply the negation is inside the brackets.

  • wayne

    Hi Alex,

    I thought I would revisit question 6 from section 3.7 and do it using what I have learnt in sections 3.8 and 3.8a. So here goes nothing ;-) I think my code my be a little inelegant, but I just wanted to play around with the concept of bit flags (which I find very cool). I don't really have a question here, just wanted to share my alternative answer to question 6 from section 3.7 here.

    #include <iostream>
    #include <bitset>

    int main()
    {
        const unsigned char bitNum_0 = 1 << 0;  // 0000 0001
        const unsigned char bitNum_1 = 1 << 1;  // 0000 0010
        const unsigned char bitNum_2 = 1 << 2;  // 0000 0100
        const unsigned char bitNum_3 = 1 << 3;  // 0000 1000
        const unsigned char bitNum_4 = 1 << 4;  // 0001 0000
        const unsigned char bitNum_5 = 1 << 5;  // 0010 0000
        const unsigned char bitNum_6 = 1 << 6;  // 0100 0000
        const unsigned char bitNum_7 = 1 << 7;  // 1000 0000
        unsigned char myByte = 0;         // 0000 0000
        
        std::cout << "Enter an integer between 0 and 255, inclusive: ";
        int input;
        std::cin >> input;
        
        char num = static_cast<char>(input);
        
        if(bitNum_0 & num)
           myByte |= bitNum_0;
        
        if(bitNum_1 & num)
           myByte |= bitNum_1;
          
        if(bitNum_2 & num)
           myByte |= bitNum_2;
          
        if(bitNum_3 & num)
           myByte |= bitNum_3;
          
        if(bitNum_4 & num)
           myByte |= bitNum_4;
          
        if(bitNum_5 & num)
           myByte |= bitNum_5;
          
        if(bitNum_6 & num)
           myByte |= bitNum_6;
          
        if(bitNum_7 & num)
           myByte |= bitNum_7;
          
        std::cout << static_cast<int>(myByte) << std::endl;
        std::bitset<8> b;
        b = myByte;
        std::cout << b << std::endl;
        
        return 0;
    }

  • Trung

    Hi Alex,
    Could you show me how to use bitset to set on multiple bits at once? When I try using bitwise OR like bits.set(1|2), it turns to bits.set(3) so that only bit 3 is on. I'm a bit confused here :) Thanks.

    • Alex

      You can't set multiple bits at once via the set() function. I think the best way to do so might be by using the bitwise assignment operators (&= and |=).

  • James

    Is this a Monster Hunter reference? :D

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

    • Vishal Sundarrajan

      Ah! I got that. 'myflag' is the concerned variable here. I was thinking the other way. My bad. Thx for the explanation

  • 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 all code inside code tags: [code]your code here[/code]