How to write log base(2) in c/c++

c++ c
russell picture russell · Jun 17, 2010 · Viewed 220.7k times · Source

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.

Answer

Adam Crume picture Adam Crume · Jun 17, 2010

Simple math:

    log2 (x) = logy (x) / logy (2)

where y can be anything, which for standard log functions is either 10 or e.