log2
In Python, there isn't a function equivalent to log2(3). You say
import math def log2(n): math.log(n) / math.log(2)
to get x in n = 2**x. I didn't know but it's quite common. I must study mathematic more.
But I guess it is slow because it contains divisions. When you want to know logrithm of n in 2 by integer, for instance shortest bit size of integer n,
int int_log2(char n){ int mostleftbit = 7; while(mostleftbit >= 0){ if(n == (0x01 << mostleftbit)){ return mostleftbit; } if(n & (0x01 << mostleftbit)){ return mostleftbit + 1; } mostleftbit--; } return 0; }
does well. int_log2(5) returns 3 and int_log2(2) returns 1. Note that the result is the ceil of log(n) / log(2).