Here's a bona fide loop-free answer. It might be quicker for some inputs, but obviously slower for 1, 2, 4, 8, 16. May be faster for something like 128 or 256 and higher.

Code:

int pow = 0;
unsigned long tmp;
/* Let's assume num is an unsigned long; assume
unsigned longs are 32 bits :-) */
if (tmp = num >> 16) {
pow |= 16;
num = tmp;
}
if (tmp = num >> 8) {
pow |= 8;
num = tmp;
}
if (tmp = num >> 4) {
pow |= 4;
num = tmp;
}
if (tmp = num >> 2) {
pow |= 2;
num = tmp;
}
if (tmp = num >> 1) {
pow |= 1;
num = tmp;
}
/* pow now contains the logarithm. */

Of course, you could just roll that into a loop:

Code:

int pow = 0;
unsigned long tmp; /* Should really be same type as num. */
int i = 32; /* Set to power of two, >= length of num in bits. */
while (i >>= 1) {
if (tmp = num >> i) {
pow |= i;
num = tmp;
}
}