C++ std::log2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. A positive number. Zero and negative numbers are outside the domain. An integer is converted to double.

Return Value

Return ValueDescription
LogarithmThe base-2 logarithm of x.
-infReturned when x is 0.
NaNReturned 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;
}

How It Works

  1. Each value in the array goes to std::log2(), which asks "which power of two is this?".
  2. 1, 2, 8, and 1024 are 20, 21, 23, and 210, so the answers are 0, 1, 3, and 10.
  3. 10 is between 8 and 16, so its log is between 3 and 4: 3.32193.

Example 2 — How Many Bits Does a Number Need

A positive whole number n needs floor(log2(n)) + 1 bits to be stored in binary.

C++
#include <cmath>
#include <iostream>

int main() {
    int numbers[] = {1, 255, 256, 1000};

    for (int n : numbers) {
        int bits = static_cast<int>(std::floor(std::log2(n))) + 1;
        std::cout << n << " needs " << bits << " bit(s)\n";
    }
    return 0;
}

How It Works

  1. The integer n is converted to a double and passed to log2(). floor() cuts off the decimal part.
  2. 255 is just below 28, so log2(255) is about 7.99, which floors to 7. Adding 1 gives 8 bits.
  3. 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;
}

How It Works

  1. log2(64) is exactly 6. It equals its own floor(), so 64 is a power of two.
  2. log2(100) is 6.64386. It does not equal its floor() of 6, so 100 is not a power of two.
  3. 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.

Next: logb()

Keep going through the <cmath> functions. The binary exponent of a number, as a double.

logb() tutorial →

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.

8 people found this page helpful