473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Beginner: Using Bitflags

Hi,

I've got a class with an Options property. I'd like to implement this
property in a way that a calling syntax like

MyClassInstance.Options = MyClass.Option1 | MyClass.Option2;

can be used. My questions with that:

1. How do I store these BitFlags internally?
2. How do I check for whether certain Bits have been set.
3. The above code shows how to switch 2 bits ON (hopefully, correct me
if I'm wrong). How would the syntax look If I'd like to switch Option1
ON and Option2 OFF?

I'd highly appreceate a short example!

Thanks in advance.

Matthias
Nov 16 '05 #1
3 4877
Try using enumerations with the FlagsAttribute class.

If Option1 and Option2 were bits, then your assumption in 3 is
incorrect. The code performs an OR on the bits, so MyClass.Options will
tell you that at least one of the is on.

This shows an example of how to use them.
http://msdn.microsoft.com/library/de...classtopic.asp

Matthias S. wrote:
Hi,

I've got a class with an Options property. I'd like to implement this
property in a way that a calling syntax like

MyClassInstance.Options = MyClass.Option1 | MyClass.Option2;

can be used. My questions with that:

1. How do I store these BitFlags internally?
2. How do I check for whether certain Bits have been set.
3. The above code shows how to switch 2 bits ON (hopefully, correct me
if I'm wrong). How would the syntax look If I'd like to switch Option1
ON and Option2 OFF?

I'd highly appreceate a short example!

Thanks in advance.

Matthias

Nov 16 '05 #2
Here's a simple way to do something like this:

const int Red = 1;
const int Green = 2;
const int Blue = 4;
const int Yellow = 8;

int intMyOptions = 0;
MessageBox.Show("Start: " + intMyOptions.ToString());

intMyOptions = (intMyOptions | Red);
MessageBox.Show("Options after first red: " + intMyOptions.ToString());

// Once bit is set, it won't unset or append
intMyOptions = (intMyOptions | Red);
MessageBox.Show("Options after second red: " + intMyOptions.ToString());

intMyOptions = (intMyOptions | Yellow);
MessageBox.Show("Options after yellow: " + intMyOptions.ToString());

string strResults = "Results:\n\n";
strResults += "Red: " + (((intMyOptions & Red) > 0)? "Y" : "N") + "\n";
strResults += "Green: " + (((intMyOptions & Green) > 0)? "Y" : "N") + "\n";
strResults += "Blue: " + (((intMyOptions & Blue) > 0)? "Y" : "N") + "\n";
strResults += "Yellow: " + (((intMyOptions & Yellow) > 0)? "Y" : "N") +
"\n";
MessageBox.Show(strResults);

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,

I've got a class with an Options property. I'd like to implement this
property in a way that a calling syntax like

MyClassInstance.Options = MyClass.Option1 | MyClass.Option2;

can be used. My questions with that:

1. How do I store these BitFlags internally?
2. How do I check for whether certain Bits have been set.
3. The above code shows how to switch 2 bits ON (hopefully, correct me
if I'm wrong). How would the syntax look If I'd like to switch Option1
ON and Option2 OFF?

I'd highly appreceate a short example!

Thanks in advance.

Matthias

Nov 16 '05 #3
The standard way is to use an enumeration with the values explicitly set so the binary representations of 0 - 8 are as follows.

0 0000

1 0001

2 0010

4 0100

8 1000

as you can see eac one sets a different bit so you can always tell which ones were selected

so declare an enum:

enum Commitment

{

Heart = 1,

Mind = 2,

Soul = 4

}

Then you can write

Commitment c = Commitment.Heart | Commitment.Soul;

Now its good practice (though not required) to decorate the enum with the

[System.Flags]

attribute. This does two things: it signals to users of the enum that it is intended to me used as a set of bitfields; it changes the way ToStriing works so it prints out a comma separated list of the combined symbols. By default enums occupy 32 bits of storage (so you can have 32 bitwise or-able values). However, you can change this storage type as follows

enum Commitment : byte // any integral type will do fine here

{

...

}

Even though it looks like derivation from a value type (which isn't supported in .NET) it simply states the underlying storage for the enum type.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardrb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#u*************@TK2MSFTNGP10.phx.gbl>

Hi,

I've got a class with an Options property. I'd like to implement this
property in a way that a calling syntax like

MyClassInstance.Options = MyClass.Option1 | MyClass.Option2;

can be used. My questions with that:

1. How do I store these BitFlags internally?
2. How do I check for whether certain Bits have been set.
3. The above code shows how to switch 2 bits ON (hopefully, correct me
if I'm wrong). How would the syntax look If I'd like to switch Option1
ON and Option2 OFF?

I'd highly appreceate a short example!

Thanks in advance.

Matthias

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Rod Carrol | last post by:
Hello all, As a beginner I've been exeperiencing lots of errors while building my website, (I'm currently attempting to implement a member login/registration piece for my site using mySQL and...
5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
2
by: Zen Masta | last post by:
An older book I have ws written with vs.net 2002 in mind and unfortunely one of my discs is scratched so I can't install anymore. I find it hard to follow because it mainly uses the command prompt...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.