Click here to Skip to main content
11,480,938 members (60,502 online)
Click here to Skip to main content

Tagged as

Create Enumeration as Bit Flags

, 26 Jan 2011 CPOL 11.5K 5
Rate this:
Please Sign up or sign in to vote.
Create Enumeration as Bit Flags

Enumeration is one of the important features 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 are 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,
        Desperate,
        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.

Hmm... Putting it further, Enum might also come in 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 do 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,
        Desperate=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 a certain flag in it.

For Further Reading

To read more about Flags attribute usage, you can go to the MSDN link and read the Usage Guidelines.

Thank you, I hope you found it interesting to read the post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhishek Sur
Architect
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

Comments and Discussions

 
Generalmy vote of 4 Pin
Mojtaba Rezaeian13-Mar-12 4:35
memberMojtaba Rezaeian13-Mar-12 4:35 
GeneralMy vote of 1 Pin
Adrian Cole7-Nov-10 10:56
memberAdrian Cole7-Nov-10 10:56 
GeneralRe: My vote of 1 Pin
shakil03040031-Feb-11 1:34
membershakil03040031-Feb-11 1:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 26 Jan 2011
Article Copyright 2010 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid