Facebook Page Twitter Page LinkedIn Page
× C Tutorial C Resources


The C math log2() function is used to return the base-2 (binary) logarithm of a given number.

Syntax

double log2(double x);
float log2f(float x);
long double log2l(long double x);

Parameters

x Specify the number.

Return Value

Returns the base-2 (binary) logarithm of a given number.
If the x is negative, domain error occurs.

Example:

In the below example, log2() function is used to calculate the base-2 (binary) logarithm of a given number.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("%lf\n", log2(2));
  printf("%lf\n", log2(32));
  printf("%lf\n", log2(50));
  return 0;
}

The output of the above code will be:

1.000000
5.000000
5.643856

❮ C <math.h> Library