Is there a formula for finding logarithms to base two of a number.If so wtat is it.Thanks for any replying.
formula for logarithms to base two of a number
Collapse
X
-
There's a simple integer approximation:
Code:int log2(unsigned int n) { int log = 0; if (n >= 1<<16) { n >>= 16; log += 16; } if (n >= 1<< 8) { n >>= 8; log += 8; } if (n >= 1<< 4) { n >>= 4; log += 4; } if (n >= 1<< 2) { n >>= 2; log += 2; } if (n >= 1<< 1) { log += 1; } return ((n == 0) ? -1 : log); }
kind regards,
JosComment
Comment