title: Testing for bitwise Enum values
body: <p>I haven't really used bitwise enums before, and I just want to make sure my testing is correct. I am most interested in testing for the values None and All. We receive data from a webservice that utilises this enum to categorise certain pieces of the data. Given that, I am assuming that nether None nor All would ever be combined with any other value. </p>
  
  <p>Given the following bitwise enum definition;</p>
  
  <pre><code>[System.FlagsAttribute()]
  public enum TrainingComponentTypes : int
      {
          None = 0,
          AccreditedCourse = 1,
          Qualification = 2,
          Unit = 4,
          SkillSet = 8,
          UnitContextualisation = 16,
          TrainingPackage = 32,
          AccreditedCourseModule = 64,
          All = 127,
      }
  </code></pre>
  
  <p>I read the following quote on <a href="http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx" rel="noreferrer" title="this MSDN siite">this MSDN site</a> about FlagAttributes;</p>
  
  <blockquote>
    <p>Use None as the name of the flag
    enumerated constant whose value is
    zero. You cannot use the None
    enumerated constant in a bitwise AND
    operation to test for a flag because
    the result is always zero. However,
    you can perform a logical, not a
    bitwise, comparison between the
    numeric value and the None enumerated
    constant to determine whether any bits
    in the numeric value are set.</p>
  </blockquote>
  
  <p>Does a <strong>logical comparison</strong> in this instance refer to a normal equality test for enums? 
  For example;</p>
  
  <pre><code>TrainingComponentTypes tct = TrainingComponentTypes.None; 
  if (tct == TrainingComponentTypes.None) 
  { ... }
  </code></pre>
  
  <p>For a <strong>bitwise comparison</strong>, I am performing the following;</p>
  
  <pre><code> TrainingComponentTypes tct = TrainingComponentTypes.AccreditedCourse | TrainingComponentTypes.Qualification | TrainingComponentTypes.TrainingPackage;
   Assert.IsTrue((tct &amp; TrainingComponentTypes.AccreditedCourse) == TrainingComponentTypes.AccreditedCourse, "Expected AccreditedCourse as part the enum");
  
   Assert.IsFalse((tct &amp; TrainingComponentTypes.SkillSet) == TrainingComponentTypes.SkillSet, "Found unexpected SkillSet as part the enum");
  </code></pre>
  
  <p>Lastly, when testing for all, I have tried both a logical, and bitwise comparison, and they both return the same. Should I be using one over the other here? For example;</p>
  
  <pre><code>TrainingComponentTypes tct = TrainingComponentTypes.All;
  
  Assert.IsTrue((tct &amp; TrainingComponentTypes.All) == TrainingComponentTypes.All, "Expected All as part the enum");
  Assert.IsTrue((tct) == TrainingComponentTypes.All, "Expected All as part the enum");
  // The follow also pass the assertion for a value of All
  Assert.IsTrue((tct &amp; TrainingComponentTypes.Qualification) == TrainingComponentTypes.Qualification, "Expected Qualification as part the enum");
  Assert.IsTrue((tct &amp; TrainingComponentTypes.TrainingPackage) == TrainingComponentTypes.TrainingPackage, "Expected TrainingPackage as part the enum");
  </code></pre>
  
  <p>So in summary, I'd like to know the following about Bitwise enums;</p>
  
  <ol>
  <li>Is my understanding of a logical
  comparison correct given my example
  above?   </li>
  <li>Is the way I am performing
  a bitwise comparison correct?   </li>
  <li>What is the right way to handle the "All"
  value (bitwise or logical). I am not sure if we'd ever receive a value where All was combined with other TrainingComponentTypes. I can't see why we would, but then, you never know?   </li>
  <li>Am I right in assuming that switch
  statements basically shouldn't be
  used for bitwise enums (given none
  is appears to be a special case and
  requires a logical comparison)?</li>
  </ol>
  
  <p>Thanks,
  Chris</p>