cc [ flags ] -I/usr/local/include file(s) -L/usr/local/lib -lmcw [ ... ] #include <mathcw.h> extern int ilog2 (unsigned int); extern int lilog2 (unsigned long int); extern int llilog2 (unsigned long long int);
On most systems, the fast algorithm used allows this to be done in a time that is fixed, rather than proportional to the number of bits in a word.
ilog2(n) = (int)floorl((long double)n) ilog2(n) = wordsize - 1 - nlz(n) ilog2(0) = -1
The book
describes several implementations of these functions, and is a general treasure trove of clever tricks with integer arithmetic.Henry S. Warren, Jr. Hacker's Delight Addison-Wesley (2003) ISBN 0-201-91465-4