473,406 Members | 2,698 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,406 software developers and data experts.

[Flags] Attribute comparison

179 100+
I should know how to do this but I unfortunately don't, so I'd just like to clarify...

I've got a Flags enum:

Expand|Select|Wrap|Line Numbers
  1. [Flags]
  2.         public enum SkillSet
  3.         {
  4.             Skill1 = 1,
  5.             Skill2 = 2,
  6.             Skill3 = 4,
  7.             Skill4 = 8,
  8.             Skill5 = 16,
  9.             Skill6 = 32,
  10.             Skill7 = 64,
  11.             Skill8 = 128
  12.         }
  13.  
I then have for example:
Expand|Select|Wrap|Line Numbers
  1. SkillSet skillsAvaliable = Skill1 && Skill2 && Skill7 && Skill6; // believe this is correct?
  2. SkillSet skillsRequired = Skill1 && Skill7;
I now want to see if all the skillsRequired are contained within skillsAvaliable. Is it possible to do this in one call? Or do I need to loop through each value of skillsRequired compare to the skillsAvaliable and return the resultant bool?
Jul 30 '08 #1
7 1523
Plater
7,872 Expert 4TB
EDIT:
I *think* you can do bit logic, something like this:
Expand|Select|Wrap|Line Numbers
  1. //given:
  2. SkillSet skillsAvaliable1 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill7 | SkillSet.Skill6;
  3. SkillSet skillsAvaliable2 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill6;
  4. SkillSet skillsRequired = SkillSet.Skill1 | SkillSet.Skill7;
  5.  
  6.             //then do this
  7. bool hasCorrectSkills1 = ((skillsAvaliable1 & skillsRequired) == skillsRequired);
  8. bool hasCorrectSkills2 = ((skillsAvaliable2 & skillsRequired) == skillsRequired);
  9. MessageBox.Show(hasCorrectSkills1.ToString() + " - " + hasCorrectSkills2.ToString()); 
You need to be using the single or '|'
Jul 30 '08 #2
TRScheel
638 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. [Flags]
  2.         public enum SkillSet
  3.         {
  4.             Skill1 = 1,
  5.             Skill2 = 2,
  6.             Skill3 = 4,
  7.             Skill4 = 8,
  8.             Skill5 = 16,
  9.             Skill6 = 32,
  10.             Skill7 = 64,
  11.             Skill8 = 128
  12.         }
  13.  
It makes perfect sense if you take a look at it from the view of bytes.

Skill1 = 0000 0001
Skill2 = 0000 0010
Skill3 = 0000 0100
Skill4 = 0000 1000
Skill5 = 0001 0000
Skill6 = 0010 0000
Skill7 = 0100 0000
Skill8 = 1000 0000

Now if skills available has 1, 2, 7, and 6 then you would have:

Skill1 | Skill2 | Skill7 | Skill 6
0000 0001
0000 0010
0010 0000
0100 0000
_________
0110 0011

SkillsAvailable = 0110 0011


Skills Required would then need Skills 1 and 7 so:

Skill1 | Skill7
0100 0000
0000 0001
_________
0100 0001

SkillsRequired = 0100 0001


Then to see if SkillsAvailable has SkillsRequired you would AND them together and then check if it matched SkillsRequired.


SkillsAvailable & SkillsRequired
0100 0001
0110 0011
__________
0100 0001

SkillsMix = 0100 0001

SkillsMix == SkillsRequired


Hope that explains it
Jul 30 '08 #3
IanWright
179 100+
TRScheel thanks for that, it makes a lot of sense if you look at it from that perspective and actually makes it seem incredibly easy!

Thanks for you response Plater, I think however I'm going to follow TRScheel's suggestion for simplicity and readability :D
Jul 30 '08 #4
Plater
7,872 Expert 4TB
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
Jul 30 '08 #5
TRScheel
638 Expert 512MB
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
Mine has bit math, hence its truthiness factor is way higher

=D
Jul 30 '08 #6
Plater
7,872 Expert 4TB
Mine has bit math, hence its truthiness factor is way higher

=D
I gave a man a fish. You taught him to fish.
Jul 30 '08 #7
IanWright
179 100+
What was the difference between what I said and what he said?
He explained how Flags attribute work in terms of binary opperations.
I showed you the code to do it :-P
Actually Plater, having looked back I believe your post is equally useful as I skimmed over it too quickly. You're explaining that I should be using the | OR rather than & AND which is vital really!

I take it back, both are good in their own way :)
Jul 30 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Carl | last post by:
Hi, I have found a way to map attributes (columns) to column headings. But this runs really slow. Is there a way to improve it? Thanks, Carl <?xml version="1.0" encoding="utf-8" ?>...
4
by: Keith Hill | last post by:
Is there a way to represent a style enum in XML Schema? Say I have a flags enum like so: public enum dayOfWeek { Mon, Tue, Wed, All = Mon | Tue | Wed; }
3
by: Patrick Blackman | last post by:
Need example of how to use the flags attribute and to determine which flags were set.
4
by: Paul E Collins | last post by:
The help file says that "bit fields can be combined using a bitwise OR operation, whereas enumerated constants cannot", and yet this code works: enum Blah { a, b, c }; // without .... Blah x...
4
by: Joel Moore | last post by:
Say I have an enum similar to this: <Flags()> Enum TestFlags As Short One = 0 Two = 1 Three = 2 Four = 4 End Enum I figured I could declare variable of type TestFlags to be used in the
2
by: Chad | last post by:
I've used bitwise enums before (powers of 2) and have used AND and OR operator to determine which individual flags have been set, but I have never used the <Flags()> _ attribute What advantage...
1
by: martin | last post by:
Hi, I have used bitfields in C before and I understand all the AND/OR operations etc. While learning C# I was told to mark all by "bitfield enums" with the attribute. Do why? Exactly what does...
2
by: Jigar.Patel | last post by:
Hi, I have following enum definition. public enum OperationTypes { All = 1, New = 2, View = 4,
20
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.