In C++, std::log2(x) is the base-2 logarithm. It tells you how many times 2 must be multiplied to reach x. Since 23 is 8, log2(8) is 3.
It is the natural logarithm for computer science. It counts bits, the depth of a binary tree, and the number of steps in a binary search. It is the opposite of std::exp2(). It was added in C++11.
std::log2() is declared in <cmath> and is part of the group Exponential and logarithm. Exponentials and logarithms undo each other. A logarithm of zero is -inf, and a logarithm of a negative number is NaN.
Syntax
Include <cmath> and call std::log2. Start with these lines.
C++
double a = std::log2(8.0); // double version
float b = std::log2(8.0f); // float version
Call
What it does
std::log2(8.0)
Passes a double and returns a double. Here the result is 3.
std::log2(8.0f)
Passes a float, so the float overload runs and returns a float.
std::log2(1024)
An integer is converted to double. Returns 10 as a double.
Write std::log2 so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float log2(float x);
double log2(double x);
long double log2(long double x);
double log2(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A positive number. Zero and negative numbers are outside the domain. An integer is converted to double.
Return Value
Return Value
Description
Logarithm
The base-2 logarithm of x.
-inf
Returned when x is 0.
NaN
Returned when x is negative.
C++ Examples
Example 1 — Base-2 Logarithms
Print log2() for a few numbers. Exact powers of two give whole results.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {1.0, 2.0, 8.0, 1024.0, 10.0};
for (double v : values) {
std::cout << "log2(" << v << ") = " << std::log2(v) << '\n';
}
return 0;
}
The integer n is converted to a double and passed to log2(). floor() cuts off the decimal part.
255 is just below 28, so log2(255) is about 7.99, which floors to 7. Adding 1 gives 8 bits.
256 is exactly 28, so it needs 9 bits. 1000 has log2 of 9.97 and needs 10 bits. The number 1 needs just 1 bit.
Example 3 — Is It a Power of Two
A number is a power of two when log2(n) has no decimal part. Compare it with its own floor().
C++
#include <cmath>
#include <iostream>
int main() {
int numbers[] = {64, 100, 1, 4096};
for (int n : numbers) {
double l = std::log2(n);
if (l == std::floor(l)) {
std::cout << n << ": yes, 2^" << l << '\n';
} else {
std::cout << n << ": no\n";
}
}
return 0;
}
Output
64: yes, 2^6
100: no
1: yes, 2^0
4096: yes, 2^12
How It Works
log2(64) is exactly 6. It equals its own floor(), so 64 is a power of two.
log2(100) is 6.64386. It does not equal its floor() of 6, so 100 is not a power of two.
1 is 20 and 4096 is 212, so both print the exponent 0 and 12. For integers a bit trick (n & (n - 1)) == 0 is faster.
Frequently Asked Questions
std::log2(x) returns the base-2 logarithm of x. It is the power you raise 2 to in order to get x, so log2(8.0) is 3.
The base-2 logarithm as float, double, or long double. log2(0) is -inf, and log2 of a negative number is NaN.
It counts bits, the number of levels in a binary tree, and the steps of a binary search. For n items, binary search needs about log2(n) steps.
Yes. Good C++ libraries return exact whole numbers for exact powers of two, such as log2(1024) equals 10. For other numbers the result is a decimal, so round it down or up as needed.
Include <cmath> and write std::log2(). That always works and picks the C++ overloads. The global name ::log2 often works too, but the standard does not promise it, and it may only take a double.