Understand bit masks in C#

[bit masks]

Some values, including some properties defined by the .NET Framework, are bit masks. That means each bit in a value means something. For example, the AnchorStyles enumeration that determines how controls are anchored in their parents defines four values: Top, Bottom, Left, and Right. Numerically those values correspond to 1, 2, 4, and 8. As binary values, they are 0001, 0010, 0100, and 1000. Notice that each value has exactly 1 bit set and that none of them have the same bit set.

Because of that, you can combined values by using bitwise operations. (See Use bit operations in C#.) For example, the value AnchorStyles.Bottom | AnchorStyles.Left is equivalent to 0010 | 0100 which equals 0110.

To set a value for bit masks such as this one, you can set the value equal to the bitwise Or of the values that you want to use. For instance, this example program uses the following code to set a PictureBox control’s Anchor property to Bottom, Right.

// Anchor the PictureBox.
private void Form1_Load(object sender, EventArgs e)
{
    // Anchor the PictureBox at the bottom and right.
    picAnchor.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
}

The way the bitwise operators work leads to some semantic confusion. This code uses the bitwise Or to create a value that means the control should be anchored on the bottom and right. This is because the bitwise Or operator results in multiple bits being set.

You also can use other bitwise operations to change the value. For example, the following code would clear the Top setting from the control’s Anchor property while leaving any other settings unchanged.

picAnchor.Anchor &= ~AnchorStyles.Top;

What this code does is And the current value with the value Not Top. The value Not Top has every bit set except where the Top value’s bit is set. In this case, that’s ~0001 or 1110. When you And this value with the property’s current value, you get the same bits set as in the current value except for the rightmost bit, which is cleared by the final bit in 1110.

Often values used in this way are called masks because they mask out one or more of the value’s bits. (It’s a little bit like using a mask or stencil with a can of spray paint to restrict where the paint goes. In this example, the 1110 mask makes the “clear the bit” paint only affect the final bit in the value. (If you don’t like analogy, ignore it.)

The following code adds the Left setting to the Anchor property’s current value.

picAnchor.Anchor |= AnchorStyles.Left;

This code uses the bitwise Or operator to set the bit corresponding to the Left value 0100. For example, if the property’s initial value is 1001, then the new value is 1001 | 0100 = 1101.

In the next post I’ll explain how you can define new fields for bit masks similar to AnchorStyles.Left in your code.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in algorithms, calculations, mathematics, variables and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *