title: Compute fast log base 2 ceiling
body: <p>What is a fast way to compute the <code>(long int) ceiling(log_2(i))</code>, where the input and output are 64-bit integers? Solutions for signed or unsigned integers are acceptable. I suspect the best way will be a bit-twiddling method similar to those found <a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogObvious" rel="nofollow noreferrer">here</a>, but rather than attempt my own I would like to use something that is already well tested. A general solution will work for all positive values.</p>
  <p>For instance, the values for 2,3,4,5,6,7,8 are 1,2,2,3,3,3,3</p>
  <p>Edit: So far the best route seems to be to compute the integer/floor log base 2 (the position of the MSB) using any number of fast existing bithacks or register methods, and then to add one if the input is not a power of two. The fast bitwise check for powers of two is <code>(n&amp;(n-1))</code>.</p>
  <p>Edit 2: A good source on integer logarithms and leading zeroes methods is Sections 5-3 and 11-4 in <a href="http://www.hackersdelight.org/" rel="nofollow noreferrer">Hacker's Delight</a> by Henry S. Warren. This is the most complete treatment I've found.</p>
  <p>Edit 3: This technique looks promising: <a href="https://stackoverflow.com/a/51351885/365478">https://stackoverflow.com/a/51351885/365478</a></p>
  <p>Edit 4: C23 is apparently adding stdc_first_(leading/trailing)_(one/zero)</p>