title: Generic Constraint for allowing bitwise operations?
body: <p>I have an <code>enum</code> : </p>
  
  <pre><code>  public enum Flags {
          COMMAND_MSG = 1,
          COMMAND_FILE = 2,
          COMMAND_ACTION = 4,
        }
  </code></pre>
  
  <p>Now , suppose I set multiple values like : </p>
  
  <pre><code>Flags g = Flags.COMMAND_ACTION |Flags.COMMAND_MSG; 
  </code></pre>
  
  <p>So I have an int with value <code>5</code>.</p>
  
  <p>Now , from that <code>5</code> I want to see that it's a combination of  : <code>Flags.COMMAND_ACTION |Flags.COMMAND_MSG;</code></p>
  
  <p><em>(<strong>Notice</strong> , I dont have <code>[Flags]</code>  attribute because I'm using protobuff library , and the enum is auto</em> generated.</p>
  
  <p>What have I tried : </p>
  
  <pre><code>public string Show (Flags item)
  {
    var s="";
    string.Join(",", Enum.GetValues(typeof(Flags))
          .Cast&lt;Flags&gt;() 
          .Select(f=&gt;(f &amp; item) &gt;0 ?f.ToString() :"") //check if bit is set
          .Where(f=&gt;!string.IsNullOrWhiteSpace(f))); //remove empty
    return s;
  }
  </code></pre>
  
  <p>So <code>Show(5);</code> does display :  <code>COMMAND_MSG,COMMAND_ACTION</code></p>
  
  <p><em>So where is the problem  ?</em></p>
  
  <p>I wanted to convert it to a generic extension method : </p>
  
  <pre><code> public static string ToFlags&lt;T&gt;(this int val, T FlagType) where T : ??
  
      {
          return string.Join(",", Enum.GetValues(typeof(T))
                       .Cast&lt;T&gt;()
                       .Select(enumEntry =&gt; (enumEntry &amp; val) &gt; 0 ? enumEntry.ToString() : "")
                       .Where(f =&gt; !string.IsNullOrWhiteSpace(f)));
      }
  </code></pre>
  
  <p>But there's an error because :</p>
  
  <p><img src="https://i.stack.imgur.com/uzI4u.png" alt="enter image description here"></p>
  
  <p><strong>Question :</strong> </p>
  
  <p>Which generic constraint should I apply in order for this to work ? </p>