C Standard Library

C <math.h> - log2() Function



The C <math.h> log2() function returns 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 example below, 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