CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    What is a BitMask

    Can anybody explain what a Bit Mask is. I have seen its usage in various structure fields.

    What are its advantages?

    Any links relating to it will be helpful.

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: What is a BitMask

    Quote Originally Posted by miteshpandey
    Can anybody explain what a Bit Mask is. I have seen its usage in various structure fields.
    Basically, a bit mask is a value which can be used to mask out certain bits of another value by applying bitwise AND, OR and XOR.

    For example, let's assume that a 16 bit value is used to store the R,G,B components of a color like this:

    The leftmost 5 bits for the red component, the next 6 bits for the green component, and the rightmost 5 bits for the blue component:

    RRRR RGGG GGGB BBBB

    In this example, the color value for yellow would be

    1111 1111 1110 0000 (binary), or FFE0 (hex), or 65504 (decimal).

    Now let's assume that you need a way to filter out the individual R,G,B components from a color: This can be done by AND-ing the following bitmasks with the color value:


    Red: 1111 1000 0000 0000 (binary) = F800 (hex) = 63488 (dec)
    Green: 0000 0111 1110 0000 (binary) = 07E0 (hex) = 2016 (dec)
    Blue: 0000 0000 0001 1111 (binary) = 001F (hex) = 31 (dec)


    As an example, consider the following code:
    Code:
      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);
    (Note the right shift required to move the resulting bits to the rightmost position).
    Quote Originally Posted by miteshpandey
    What are its advantages?
    Well, the above is just one example - there are many others. Just think of things like window styles, which are also coded as single bits in a 32 bit value. Bit masks can be used to set or clear individual styles.

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: What is a BitMask

    Thank you very much gstercken.

    I have another question for you.

    How is the presence of bit value tested?

    I have seen that inorder to test for a certain bit like selection it's used like this

    if(dwStyle & BS_SELECTION == BS_SELECTION)

    Please explain the mechanism. I have somewhere heard that this is done for compatiblity reason.

    Any help will be appreciated much.

  4. #4
    Join Date
    Apr 2002
    Location
    St.Petersburg, Russia
    Posts
    714

    Re: What is a BitMask

    In expression if(A&B==B) there are 2 operations.
    First is bitwise-and - &. Let for example A=01010101 (binary) and B=00111100. Bitwise-and result is a byte/word/dword (depending on operands) in which bit is 1 only if corresponding bits in both operands are 1. Otherwise bit is 0. So, A&B=00010100.
    Second is equality operator. You need it only if both A and B contain more than 1 nonzero bit. In example above result is not equals to any of operands. But you need to know when all nonzero bits in B are also nonzero in A.
    But if you know that there is only 1 nonzero bit in B you can write if(A&B) instead.
    With best wishes, Alexander Hritonenkov

  5. #5
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: What is a BitMask

    Thank you scrambler. Now I understand much more.

    But I am confused about one thing.

    I have read somewhere that the left variable (dwStyle) could be omitted. But for future compatibility it is kept. But if we omit dwStyle everything becomes nonsense.

    Perhaps I am mixing thing here.

  6. #6
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: What is a BitMask

    Thank you scrambler. Now I understand much more.

    But I am confused about one thing.

    I have read somewhere that the left variable (dwStyle) could be omitted. But for future compatibility it is kept. But if we omit dwStyle everything becomes nonsense.

    Perhaps I am mixing thing here.

  7. #7
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: What is a BitMask

    [QUOTE=miteshpandey]I have read somewhere that the left variable (dwStyle) could be omitted. But for future compatibility it is kept. But if we omit dwStyle everything becomes nonsense.

    Quote Originally Posted by miteshpandey
    Perhaps I am mixing thing here.
    You must indeed be mixing things... What you have in mind is probably the CWnd member function ModifyStyle(): It doesn't require you to retrieve the current style first, you simply pass the combination of styles to be set or cleared, respectively.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured