The following code calls the builtin functions for clz/ctz in GCC and, on other systems, has C versions. Obviously, the C versions are a bit suboptimal if the system has a
If MSVC has a compiler intrinsic for this, it'll be here:
Compiler Intrinsics on MSDN
Otherwise, you'll have to write it using __asm