title: Find most significant bit in Swift
body: <p>I need to find the value (or position) of the <a href="https://en.wikipedia.org/wiki/Most_significant_bit" rel="nofollow noreferrer">most significant bit (MSB)</a> of an integer in Swift.</p>
  
  <p>Eg:</p>
  
  <ul>
  <li><strong>Input number:</strong> 9</li>
  <li><strong>Input as binary:</strong> 1001</li>
  <li><strong>MS <em>value</em> as binary:</strong> 1000 -> (which is 8 in decimal)</li>
  <li><strong>MS <em>position</em> as decimal:</strong> 3 (because <code>1&lt;&lt;3 == 1000</code>)</li>
  </ul>
  
  <p>Many processors (Intel, AMD, ARM) have instructions for this. In c, <a href="https://stackoverflow.com/a/673781/2547229">these are exposed</a>. Are these instructions similarly available in Swift through a library function, or would I need to <a href="https://stackoverflow.com/a/671905/2547229">implement some bit twiddling</a>?</p>
  
  <p>The value is more useful in my case.</p>
  
  <p>If a position is returned, then the value can be easily derived by a single shift.</p>
  
  <p>Conversely, computing position from value is not so easy unless a fast <a href="https://en.wikipedia.org/wiki/Hamming_weight" rel="nofollow noreferrer">Hamming Weight / pop count</a> function is available.</p>