title: Getting bit-pattern of bool in Swift
body: <p>In ObjC, a bool's bit pattern could be retrieved by casting it to a UInt8.</p>
  
  <p>e.g.</p>
  
  <ul>
  <li>true => 0x01</li>
  <li>false => 0x00</li>
  </ul>
  
  <p>This bit pattern could then be used in further bit manipulation operations.</p>
  
  <hr>
  
  <p>Now I want to do the same in Swift.</p>
  
  <p>What I got working so far is</p>
  
  <pre><code>UInt8(UInt(boolValue))
  </code></pre>
  
  <p>but this doesn't look like it is the preferred approach.</p>
  
  <p>I also need the conversion in O(1) without data-dependent branching. So, stuff like the following is not allowed.</p>
  
  <pre><code>boolValue ? 1 : 0
  </code></pre>
  
  <p>Also, is there some documentation about the way the UInt8 and UInt initializers are implemented? e.g. if the UInt initializer to convert from bool uses data-dependent branching, I can't use it either.</p>
  
  <p>Of course, the fallback is always to use further bitwise operations to avoid the bool value altogether (e.g. <a href="https://stackoverflow.com/questions/3912112/check-if-a-number-is-non-zero-using-bitwise-operators-in-c">Check if a number is non zero using bitwise operators in C</a>).</p>
  
  <hr>
  
  <ul>
  <li>Does Swift offer an elegant way to access the bit pattern of a Bool / convert it to UInt8, in O(1) without data-dependent branching?</li>
  </ul>