title: Is there another way to test Enum bit fields?
body: <p>When using Enums with bit fields:</p>
  
  <pre><code>   enum  ReallyBigEnum  { FirstChoice = 0x01, AnotherOption = 0x02 }
     ReallyBigEnum  flag = ReallyBigEnum.FirstChoice | ReallyBigEnum.AnotherOption;
  </code></pre>
  
  <p>the code used to test the bits is:</p>
  
  <pre><code>   if( (flag &amp; ReallyBigEnum.AnotherOption) == ReallyBigEnum.AnotherOption ) { ... }
  </code></pre>
  
  <p>which seems verbose and error prone because of the need to repeat the bit
  being tested.</p>
  
  <p>It would be nice if there were some way to say:</p>
  
  <pre><code>   if( flag.IsSet( ReallyBigEnum.AnotherOption ) ) { ... }
  </code></pre>
  
  <p>but Enums don't support instance methods. So, I tried a template function:</p>
  
  <pre><code>   class Enums
     {
        public static bool IsSet&lt;T&gt;( T flag, T bit ) { return (flag &amp; bit) == bit; }
     }
  </code></pre>
  
  <p>but the code to test bits then looks like this:</p>
  
  <pre><code>   if( Enums.IsSet&lt;ReallyBigEnum&gt;( flag, ReallyBigEnum.AnotherOption ) ) { ... }
  </code></pre>
  
  <p>which is a lot to write. Then I tried to shorten it:</p>
  
  <pre><code>   class Enums
     {
        public static bool IsSet( int flag, int bit ) { return (flag &amp; bit) == bit; }
     }
  </code></pre>
  
  <p>but then you have to cast each value to its base type like this:</p>
  
  <pre><code>   if( Enums.IsSet( (int)flag, (int)ReallyBigEnum.AnotherOption ) ) { ... }
  </code></pre>
  
  <p>which is also a pain to code and loses the benefit of type checking.</p>
  
  <p>The same function could be written to use 'object' parameters, but then
  the object type and underlying base type would have to be tested.</p>
  
  <p>So, I'm stuck with the standard, redundant way at the top.</p>
  
  <p>Does anyone have other ideas on a clean, simple way of testing Enum bit fields?</p>
  
  <p>Much Thanks.</p>