c++c++11bitmask

How can I use a bitmask?


How do I use it in C++? When is it useful to use?

What would be an example of a problem where a bitmask is used to see how it actually works?


Solution

  • Bit masking is "useful" to use when you want to store (and subsequently extract) different data within a single data value.

    An example application I've used before is imagine you were storing colour RGB values in a 16 bit value. So something that looks like this:

    RRRR RGGG GGGB BBBB
    

    You could then use bit masking to retrieve the colour components as follows:

      const unsigned short redMask   = 0xF800;
      const unsigned short greenMask = 0x07E0;
      const unsigned short blueMask  = 0x001F;
    
      unsigned short lightGray = 0x7BEF;
    
      unsigned short redComponent   = (lightGray & redMask) >> 11;
      unsigned short greenComponent = (lightGray & greenMask) >> 5;
      unsigned short blueComponent =  (lightGray & blueMask);