You could do it like this:
unsigned int log2 = 0; while (x != 1) { x >>= 1; log2++; } unsigned int x_log2 = log2;
The log2 value will be rounded down.