Find the highest order bit in C
From Hacker's Delight:
int hibit(unsigned int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return n - (n >> 1);
}
This version is for 32-bit ints, but the logic can be extended for 64-bits or higher.
fls
bottoms out to a hardware instruction on many architectures. I suspect this is probably the simplest, fastest way of doing it.
1<<(fls(input)-1)