C log2() Function

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.
| Call | What 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
double log2(double x);
float log2f(float x);
long double log2l(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. A positive double. Zero and negatives are outside the normal domain. |
Return Value
| Return Value | Description |
|---|
| double | The 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.
#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
- log2(1). Any log of 1 is 0.
- log2(2). 2^1 is 2, so the answer is 1.
- log2(16). 2^4 is 16, so the answer is 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.
#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
- Take log2. That finds the binary scale of
n. - Floor it. Drop the fractional part of the exponent.
- Add one. Bit length is one more than the highest set bit index.
- 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).
#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;
}
log2 = 3.5849625007
log/log2 = 3.5849625007
How It Works
- Same math idea. Change-of-base says log2(x) = log(x) / log(2).
- Call log2. The dedicated function is clearer and often better tuned.
- Compare results. Both prints should match closely.
- 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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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