C++ Program to calculate the base 2 logarithm of the given value


In different applications calculating logarithms for base 2 is somewhat necessary. There are a few shortcut methods to remember a few log values for competitive exams. While using programming, we have a handful of options to calculate logarithm results from library functions and also some tricks to compute them. In this article, we shall discuss a few techniques to compute logarithm base 2 for a given number in C++.

Using log2() function

The log2() is one library function that is used to calculate the logarithm base 2 for a given parameter. The answer may be an integer or floating point number. Using this method is very easy, it needs the cmath library to import this function then call it simply with a numeric parameter, and it will automatically compute the log base 2 for that number. Let us see the syntax and corresponding program to see its usage.

Syntax

#include < cmath >
Log2( <number> )

Algorithm

  • Take a number x as input.
  • Use log2( x ) to calculate the log base 2 for x.
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( int x ) { float answer; answer = log2( x ); return answer; } int main() { cout << "Log base 2 for input x = 32 is: " << solve( 32 ) << endl; cout << "Log base 2 for input x = 1024 is: " << solve( 1024 ) << endl; cout << "Log base 2 for input x = 568 is: " << solve( 568 ) << endl; cout << "Log base 2 for input x = 1059 is: " << solve( 1059 ) << endl; }

Output

Log base 2 for input x = 32 is: 5
Log base 2 for input x = 1024 is: 10
Log base 2 for input x = 568 is: 9.14975
Log base 2 for input x = 1059 is: 10.0485

Using logarithm function with some other base

Logarithm has a few interesting properties. From any base, we can compute the result of the logarithm in another base. To compute log2(𝑥) using any log base say 𝑘, the following formula is being used.

$$\log_{2}({x})\:=\:\frac{\log_{k}{x}}{\log_{k}{2}}$$

Algorithm

  • Take a number x as input.
  • nume := log-base-k ( x ).
  • deno := log-base-k(2).
  • Return ( nume / deno ).

Example

#include <iostream> #include <cmath> using namespace std; float solve( int x ) { float nume, deno; nume = log( x ); deno = log( 2 ); return nume / deno; } int main() { cout << "Log base 2 for input x = 32 is: " << solve( 32 ) << endl; cout << "Log base 2 for input x = 1024 is: " << solve( 1024 ) << endl; cout << "Log base 2 for input x = 568 is: " << solve( 568 ) << endl; cout << "Log base 2 for input x = 1059 is: " << solve( 1059 ) << endl; }

Output

Log base 2 for input x = 32 is: 5
Log base 2 for input x = 1024 is: 10
Log base 2 for input x = 568 is: 9.14975
Log base 2 for input x = 1059 is: 10.0485

Using Bitwise operator

Log base 2 can also be calculated using the bitwise shift operator, but this will only work for integers, or the result will be floor value. We know that power is multiplying a number by itself multiple times. Here we do the reverse, divide the number multiple times by 2, and count how many times we can divide and the result is 1 or more. Now dividing a number by 2 is similar to shifting toward the right by 1 bit, so this can be done using the bitwise shift operator, which is a faster solution than shifting. Let us see the algorithm for a better understanding

Algorithm

  • Take a number x as input.
  • count := -1.
  • while x is non-zero, do.
    • x := x after shifting 1 bit toward the right.
    • count := count + 1.
  • return count.

Example

#include <iostream> #include <cmath> using namespace std; float solve( int x ) { int count = -1; while ( x > 0 ) { count++; x = x >> 1; } return count; } int main() { cout << "Log base 2 for input x = 32 is: " << solve( 32 ) << endl; cout << "Log base 2 for input x = 1024 is: " << solve( 1024 ) << endl; cout << "Log base 2 for input x = 568 is: " << solve( 568 ) << endl; cout << "Log base 2 for input x = 1059 is: " << solve( 1059 ) << endl; }

Output

Log base 2 for input x = 32 is: 5
Log base 2 for input x = 1024 is: 10
Log base 2 for input x = 568 is: 9
Log base 2 for input x = 1059 is: 10

See, in this output, for the last two inputs, the result is in integer format, and these are the floor value for the actual log-base 2 result. So the shifting operator can only work on integer-based results.

Conclusion

Computing log-base 2 can be done using the cmath library-based log2() method. This will return the result in an integer or fraction. Another method can be using another log base with a little log formula as shown in the second section. The third method that we have discussed that is using a bitwise shift operator. We shift the result towards the right one by one and increase the counter to get the final result when the number is reaching 0. This method is faster but can only work well when we need an integer or floor value result. The fractional part is removed in this solution. We can also use the numerical method to compute the logarithm result by using bisection, the Newton-Raphson, or any other nonlinear equation-solving method to get more precise results.

Updated on: 17-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements