c++clogarithm

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


Is there any way to write log2() function?

The C language has 2 built in functions

  1. log which is base e.
  2. log10 base 10;

But I need log function of base 2. How to calculate this?


Solution

  • Simple math:

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

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