[Flags] Attribute comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanWright
    New Member
    • Jan 2008
    • 179

    [Flags] Attribute comparison

    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:

    Code:
    [Flags]
            public enum SkillSet
            {
                Skill1 = 1,
                Skill2 = 2,
                Skill3 = 4,
                Skill4 = 8,
                Skill5 = 16,
                Skill6 = 32,
                Skill7 = 64,
                Skill8 = 128
            }
    I then have for example:
    Code:
    SkillSet skillsAvaliable = Skill1 && Skill2 && Skill7 && Skill6; // believe this is correct?
    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?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    EDIT:
    I *think* you can do bit logic, something like this:
    [code=c#]
    //given:
    SkillSet skillsAvaliable 1 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill7 | SkillSet.Skill6 ;
    SkillSet skillsAvaliable 2 = SkillSet.Skill1 | SkillSet.Skill2 | SkillSet.Skill6 ;
    SkillSet skillsRequired = SkillSet.Skill1 | SkillSet.Skill7 ;

    //then do this
    bool hasCorrectSkill s1 = ((skillsAvaliab le1 & skillsRequired) == skillsRequired) ;
    bool hasCorrectSkill s2 = ((skillsAvaliab le2 & skillsRequired) == skillsRequired) ;
    MessageBox.Show (hasCorrectSkil ls1.ToString() + " - " + hasCorrectSkill s2.ToString()); [/code]
    You need to be using the single or '|'

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      Originally posted by IanWright
      Code:
      [Flags]
              public enum SkillSet
              {
                  Skill1 = 1,
                  Skill2 = 2,
                  Skill3 = 4,
                  Skill4 = 8,
                  Skill5 = 16,
                  Skill6 = 32,
                  Skill7 = 64,
                  Skill8 = 128
              }
      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

      Comment

      • IanWright
        New Member
        • Jan 2008
        • 179

        #4
        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

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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

          Comment

          • TRScheel
            Recognized Expert Contributor
            • Apr 2007
            • 638

            #6
            Originally posted by Plater
            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
            Last edited by TRScheel; Jul 30 '08, 03:32 PM. Reason: Added =D to emphasize sarcasm

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by TRScheel
              Mine has bit math, hence its truthiness factor is way higher

              =D
              I gave a man a fish. You taught him to fish.

              Comment

              • IanWright
                New Member
                • Jan 2008
                • 179

                #8
                Originally posted by Plater
                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 :)

                Comment

                Working...