C log2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, log2() returns the base-2 logarithm of a positive double. That means log2(x) answers “2 to what power gives x?” So log2(8) is 3 because 23 is 8.

You pass one positive double. The result is a double. Include <math.h>. On GCC, link with -lm. For natural or common logs, see log() and log10().

Base-2 logs show up when you talk about bits, binary trees, and powers of two. If x is an exact power of two, log2(x) is a whole number.

Syntax

Pass a positive floating-point number. Start with this call.

C
log2(8.0);
CallWhat it does
log2(8.0)Returns 3.0 because 2 cubed is 8.

Include <math.h>. On GCC, link with -lm. The argument must be positive.

Library declarations
C
double log2(double x);
float log2f(float x);
long double log2l(long double x);

Parameters

ParameterDescription
xRequired. A positive double. Zero and negatives are outside the normal domain.

Return Value

Return ValueDescription
doubleThe exponent y such that 2^y equals x (within floating-point limits).

C Examples

Example 1 — Powers of Two

Check a few exact powers of two.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", log2(1.0));
    printf("%.1f\n", log2(2.0));
    printf("%.1f\n", log2(16.0));
    return 0;
}

How It Works

  1. log2(1). Any log of 1 is 0.
  2. log2(2). 2^1 is 2, so the answer is 1.
  3. log2(16). 2^4 is 16, so the answer is 4.
  4. Whole numbers. Exact powers of two map to integers.

Example 2 — Rough Bit Length

Estimate how many bits are needed to represent a positive integer value.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double n = 1000.0;
    double bits = floor(log2(n)) + 1.0;

    printf("about %.0f bits\n", bits);
    return 0;
}

How It Works

  1. Take log2. That finds the binary scale of n.
  2. Floor it. Drop the fractional part of the exponent.
  3. Add one. Bit length is one more than the highest set bit index.
  4. Useful idea. Binary search depth and bit packing use similar math.

Example 3 — Relate to Natural Log

Show that log2(x) matches log(x) / log(2).

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 12.0;
    printf("log2      = %.10f\n", log2(x));
    printf("log/log2  = %.10f\n", log(x) / log(2.0));
    return 0;
}

How It Works

  1. Same math idea. Change-of-base says log2(x) = log(x) / log(2).
  2. Call log2. The dedicated function is clearer and often better tuned.
  3. Compare results. Both prints should match closely.
  4. Prefer log2. Use the named function when you mean base 2.

Frequently Asked Questions

log2(x) returns the base-2 logarithm of x. log2(8) is 3.
x is required. It must be a positive double.
A double y such that 2^y equals x (within floating-point limits).
Include <math.h>. On GCC, link with -lm.
log() is natural log (base e). log2() is base 2.

Next: logb()

Extract the exponent of a floating-point value.

logb() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful