title: Declaring and using a bit field enum in Swift
body: <p>How should bit fields be declared and used in Swift?</p>
  
  <p>Declaring an enum like this does work, but trying to OR 2 values together fails to compile:</p>
  
  <pre><code>enum MyEnum: Int
  {
      case One =      0x01
      case Two =      0x02
      case Four =     0x04
      case Eight =    0x08
  }
  
  // This works as expected
  let m1: MyEnum = .One
  
  // Compiler error: "Could not find an overload for '|' that accepts the supplied arguments"
  let combined: MyEnum = MyEnum.One | MyEnum.Four
  </code></pre>
  
  <p>I looked at how Swift imports Foundation enum types, and it does so by defining a <code>struct</code> that conforms to the <code>RawOptionSet</code> protocol:</p>
  
  <pre><code>struct NSCalendarUnit : RawOptionSet {
      init(_ value: UInt)
      var value: UInt
      static var CalendarUnitEra: NSCalendarUnit { get }
      static var CalendarUnitYear: NSCalendarUnit { get }
      // ...
  }
  </code></pre>
  
  <p>And the <code>RawOptionSet</code> protocol is:</p>
  
  <pre><code>protocol RawOptionSet : LogicValue, Equatable {
      class func fromMask(raw: Self.RawType) -&gt; Self
  }
  </code></pre>
  
  <p>However, there is no documentation on this protocol and I can't figure out how to implement it myself. Moreover, it's not clear if this is the official Swift way of implementing bit fields or if this is only how the Objective-C bridge represents them.</p>