Quelle est une bonne routine de bit-twiddling pour convertir un nombre dans la gamme [2^N, 2^(N-1) -1] en N?Bit twiddling en C: comment convertir 2^N en N?
Quelques exemples:
- f (1) -> 0
- f ([2-3]) -> 1
- f ([4-7]) -> 2
- f ([8-15]) -> 3
Voici une mise en œuvre:
uint f(uint num)
{
for (uint shifts = 0; num; shifts++)
num >>= 1;
return (shifts - 1);
}
http://en.wikipedia.org/wiki/Binary_logarithm – kennytm
double possible de [Comment obtenir lg2 d'un nombre qui est 2^k] (http://stackoverflow.com/questions/2213825/ comment-obtenir-lg2-d'-un-nombre-que-est-2k) – kennytm
voulez-vous dire "gamme [2^N-1, 2^(N-1)]"? – pmg