C++ std::exp2() Function

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

Definition

In C++, std::exp2(x) calculates 2 to the power of x. It is the quick way to get powers of two such as 8, 1024, or 0.5. Powers of two are everywhere in computing: bits, memory sizes, and binary trees.

It is the opposite of std::log2(), and it is more exact than std::pow(2.0, x). The result is a decimal type, so std::exp2(10) gives the double 1024. It was added in C++11.

std::exp2() 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::exp2. Start with these lines.

C++
double a = std::exp2(3.0);    // double version
float  b = std::exp2(3.0f);   // float version
CallWhat it does
std::exp2(3.0)Passes a double and returns a double. Here the result is 8.
std::exp2(3.0f)Passes a float, so the float overload runs and returns a float.
std::exp2(10)An integer is converted to double. Returns 1024 as a double.

Write std::exp2 so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       exp2(float x);
double      exp2(double x);
long double exp2(long double x);
double      exp2(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The exponent. Whole or fractional, positive or negative. An integer is converted to double.

Return Value

Return ValueDescription
2xTwo raised to the power x, always greater than zero.
infThe result is too big. For a double this happens for x of about 1024 or more.
0The result is too small to store, such as exp2(-1100).

C++ Examples

Example 1 — Powers of Two

Print 2 to a few powers, including a fraction and a negative power.

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

int main() {
    double values[] = {0.0, 3.0, 10.0, -1.0, 0.5};

    for (double v : values) {
        std::cout << "exp2(" << v << ") = " << std::exp2(v) << '\n';
    }
    return 0;
}

How It Works

  1. Each value in the array is the power, and std::exp2() returns 2 raised to it.
  2. exp2(3) is 2 * 2 * 2, which is 8, and exp2(10) is 1024. Zero gives 1.
  3. A negative power flips the number: exp2(-1) is 0.5. A half power is a square root, so exp2(0.5) prints 1.41421.

Example 2 — How Many Values Fit in n Bits

With n bits you can store 2n different values. This program shows it for three common sizes.

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

int main() {
    std::cout << std::fixed << std::setprecision(0);

    for (int bits : {8, 16, 32}) {
        std::cout << bits << " bits: " << std::exp2(bits) << " values\n";
    }
    return 0;
}

How It Works

  1. std::fixed with setprecision(0) prints a big number in full, without e+09 notation, and with no decimal point.
  2. A byte has 8 bits, and exp2(8) is 256. Two bytes give 65536 values.
  3. With 32 bits the count is 4294967296. The loop shows how fast the numbers grow with each added bit.

Example 3 — Money That Doubles Every 7 Years

If money doubles every 7 years, the amount after t years is 1000 * exp2(t / 7).

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

int main() {
    double start = 1000.0;
    double period = 7.0;

    std::cout << std::fixed << std::setprecision(2);
    for (double years : {7.0, 14.0, 21.0, 3.5}) {
        std::cout << years << " years: " << start * std::exp2(years / period) << '\n';
    }
    return 0;
}

How It Works

  1. years / period counts how many doubling periods have passed. After 7 years that is 1.
  2. exp2(1) is 2, so the money is 2000. After 14 and 21 years it is 4000 and 8000.
  3. The exponent can be a fraction. At 3.5 years the exponent is 0.5, and exp2(0.5) is 1.41421, giving 1414.21.

Frequently Asked Questions

std::exp2(x) returns 2 raised to the power x. For example exp2(3.0) is 8 and exp2(10.0) is 1024.
Two to the power x as float, double, or long double. A negative x gives a fraction, and a very large x gives inf.
It is usually faster and more exact, and its name says what it does. Use exp2(x) when the base is always 2.
Use a shift: 1 << n gives 2 to the power n as an int. Keep n smaller than the number of bits in the type. Use exp2() when the exponent can be a fraction or negative.
Include <cmath> and write std::exp2(). That always works and picks the C++ overloads. The global name ::exp2 often works too, but the standard does not promise it, and it may only take a double.

Next: expm1()

Keep going through the <cmath> functions. e to the power x, minus 1, with high accuracy.

expm1() 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.

14 people found this page helpful