title: Find the most significant bit or log base 2 (floor) of a positive integer via straight line bit manipulation in C
body: <p>This is what I need to do:</p>
  
  <pre><code>int lg(int v)
  {
      int r = 0;
      while (v &gt;&gt;= 1) // unroll for more speed...
      {
          r++;
      }
  }
  </code></pre>
  
  <p>I found the above solution at: <a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog" rel="nofollow">http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog</a></p>
  
  <p>This works, butI need to do it without loops, control structures, or constants bigger than 0xFF (255), which has proven to be very hard for me to find.  I've been trying to figure something out using conditionals in the form</p>
  
  <pre><code>( x ? y : z ) = (((~(!!x) + 1)) &amp; y) | ((~(~(!!x) + 1)) &amp; z)
  </code></pre>
  
  <p>but I can't get it to work.  Thanks for your time.</p>