How to Create Enumeration as Bit Flags in C#

No.of Views2597
Bookmarked1 times
Downloads 
Votes0
By  abhi2434   On  17 Nov 2010 10:11:35
Tag : CSharp , Miscellaneous
Through this article, i will show you how to create enumeration as Bit Flags in C#.Enumeration is one the important feature of C#. Enumeration allows you to name a variable to make sense for the program
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Through this article, i will show you how to create enumeration as Bit Flags in C#.Enumeration is one the important feature of C#. Enumeration allows you to name a variable to make sense for the program.It is in general impossible to understand numeric status values when there is more than two values for a single state of object.  In such a case we give a logical name for the numeric integer and use the Logical name of it to call that particular state.

For instance :
Say you want to define the nature of a person.

public enum Character
 {
       Caring,
        Honest,
        Loving,
        Desparate,
        Obedient, 
        Logical,
        Practical
}

 You might name these characteristics using numeric values, say you have Honest as 1, Loving as 2 etc. But it would be more logical to use an Enum instead.

Putting it further, Enum might also come very handy when you want to use a combination of the same. Say for instance, a person can be both Logical and Caring. Now how could you define this? Do you need to define enumeration values for each of them ? I guess that would not be a good choice either.

Flags attribute in Enum plays a vital role if you want to use the values of an enumeration in combinations. So that each combination of enumeration values are mutually exclusive and does not overlap with another value of Enum. For bit fields, it is very easy and yet very handy to use Flags attribute rather than using your own logic to define the flags yourself.

Steps to create BitField Enumeration

  • Define each enumeration and set the value of each enumeration to be a power of 2.
  • Put a Flags attribute for the enumeration.

Yes it is so simple

[Flags]
public enum Character : int{
       Caring =0,
        Honest=1,
        Loving=2,
        Desparate=4,
        Obedient=8, 
        Logical=16,
        Practical=32}

Hence the only thing that you need to do to declare a BitField in .Net is to declare the values of enumeration as a multiple of 2 and apply a Flags attribute to the type. Hence the Character enumeration can now act in Bitwise Operators. The values of the Enumeration will look like.

00000000  0
00000001  1
00000010  2
00000100  4
00001000  16
00010000  32
00100000  64
01000000  128 

 Add Flag Combinations

To add more than one flag you could use | operator or bitwise OR operator. Say for instance you want to define a character which is both Caring, Logical and Practical.  In such a case you could easily declare.

Character mycharacter = Character.Caring | Character.Logical | Character.Practical;

Here the mycharacter variable will combine each of the values of Caring, Logical and Practical using bitwise OR operator.

Checking Flag Contains

& Operator in Bitfield can be used to check whether the BitField contains the Flag. Say for instance :

Character mycharacter = Character.Caring | Character.Logical | Character.Practical;
if ((mycharacter & Character.Caring) == Character.Caring)
    Console.WriteLine("The man is caring");

Here the bitwise operator & allows you to compare if mycharacter has certain flag in it.

Note :
In case of dealing with Bit Flags, it is always a good idea to avoid using 0 as a valid value for the Enum. In our case, say I use Character.Caring as my first value.  Now,

Character newchar = new Character()
Console.WriteLine(newchar.HasFlag(Characters.Caring));

Returns true, as 0 is the initial value for any Enumeration.
Another thing that you should remember,
newchar & Character.Caring = Character.Caring

Thus you cannot separate the value Character.Caring from the enumeration. So it would be better to use None as 0 for Flags enumeration.

References

http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx

Conclusion

Through this article, you learned, how to create enumeration as Bit Flags in C#.Enumeration is one the important feature of C#. Enumeration allows you to name a variable to make sense for the program.Thank you

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</