Posted by: Cirilo Meggiolaro | 01/19/2009

Tip of the day #97 – Bit flag enumerators

Imagine the following scenario: Your application has an object with four Boolean properties, such as a CRUD (Create, Read, Update and Delete access rights) object.

Great but what if you could store the four Boolean values in a single object and still have all the feature of an enumerator? Yes, you can do that using bit flag enumerators.

The idea is to create an enumerator type that allows a variable to store more than one enumerator item.

The enumerator type

To enable an enumerator type to store flags you need to add the Flag attribute to the enumerator as following:

    [Flags]
    public enum AccessRights
    {
        Create = 1,
        Read = 2,
        Update = 4,
        Delete = 8,
        All = Create | Read | Update | Delete
    }

You may have noticed the values for each enumerator item. The values must be set using the pattern described below (numeric and binary form):

1 – 00000001

2 – 00000010

4 – 00000100

8 – 00001000

16 – 00010000

32 – 00100000

64 – 01000000

128 – 10000000

Assigning a value

To assign a value to a bit flag enumerator object, we are going to use the bitwise OR operator:

AccessRights rights = AccessRights.Create | AccessRights.Read | AccessRights.Update;

As the same way you may add more flags to the object as the same way you may concatenate strings or increment a variable:

AccessRights rights = AccessRights.Create;
rights |= AccessRights.Read;
rights |= AccessRights.Update;

Condition statement

To check if a variable has a specific flag you need to perform not a regular Boolean expression but use bitwise operators to achieve that.  The following table demonstrates how the comparison is done when we use the bitwise operator &:

First         Second     Result

False         False            False

False         True             False

True          False            False

True          True             True

Using the variable created on the previous step as example and the table above, to check if the Create flag has been added we need to use the following code snippet:

(rights & AccessRights.Create) == AccessRights.Create)

Displaying the items

If you are performing if statements, display the items it is pretty straightforward. Using the ToString() method you are going to display the name of each flag, separated by comma.

AccessRights rights = AccessRights.Create | AccessRights.Read | AccessRights.Update;

Console.WriteLine(“Create access: {0}”, ((rights & AccessRights.Create)== AccessRights.Create));
Console.WriteLine(“Update access: {0}”, ((rights & AccessRights.Update) == AccessRights.Update));
Console.WriteLine(“Read access: {0}”, ((rights & AccessRights.Read) == AccessRights.Read));
Console.WriteLine(“Delete access: {0}”, ((rights & AccessRights.Delete) == AccessRights.Delete));

Console.WriteLine(rights.ToString());

Output

Create access: True
Update access: True
Read access: True
Delete access: False
Create, Read, Update

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: