A version in C using successive approximation:
unsigned int getMsb(unsigned int n){ unsigned int msb = sizeof(n) * 4; unsigned int step = msb; while (step > 1) { step /=2; if (n>>msb) msb += step; else msb -= step; } if (n>>msb) msb++; return (msb - 1);}
Advantage: the running time is constant regardless of the provided number, as the number of loops are always the same.( 4 loops when using "unsigned int")