How to write log base(2) in c/c++
Is there any way to write log(base 2) function?
The C language has 2 built in function -->>
1.log
which is base e.
2.log10
base 10;
But I need log function of base 2.How to calculate this.
Solution 1:
Simple math:
log2 (x) = logy (x) / logy (2)
where y can be anything, which for standard log functions is either 10 or e.
Solution 2:
C99 has log2
(as well as log2f
and log2l
for float and long double).
Solution 3:
If you're looking for an integral result, you can just determine the highest bit set in the value and return its position.