Skip to content
C++ Better Explained
Go back
C++ Exponents: Why ^ Isn't Power and How to Use pow()
Edit page

C++ Exponents and Powers

Coming from Python or a calculator, you might reach for 2 ^ 3 to mean “2 to the power of 3.” In C++ that quietly gives you the wrong answer — because ^ is not exponentiation. Let’s see why, then do exponents the right way.


The Trap: ^ Is Not Power

In C++, ^ is the bitwise XOR operator. It compiles fine and runs without error, which is exactly what makes it dangerous:

#include <iostream>

int main() {
    std::cout << (2 ^ 3) << "\n";  // 1, NOT 8 — ^ is bitwise XOR
    return 0;
}

There’s no warning and no crash — just a silently wrong result. This is one of the most common surprises for beginners moving to C++ from another language.


The Right Way: pow()

C++ puts exponentiation in a function called pow, found in the <cmath> header:

#include <iostream>
#include <cmath>

int main() {
    double result = std::pow(2, 3);  // 8
    std::cout << result << "\n";
    return 0;
}

std::pow(base, exponent) raises base to exponent. It returns a double, which matters for the next section.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The Precision Trap with Integers

Because pow works in floating point, an answer that should be a clean integer can occasionally come back slightly off — like 99.99999 instead of 100. Casting straight to int then truncates down to the wrong value:

#include <iostream>
#include <cmath>

int main() {
    int risky = static_cast<int>(std::pow(10, 2));  // usually 100, but risky
    std::cout << risky << "\n";
    return 0;
}

On most systems this prints 100, but relying on it for large exponents is asking for a hard-to-find bug. If you need an exact integer, round it: std::round(std::pow(10, 2)).


Exact Integer Powers: A Simple Loop

For guaranteed-exact integer powers, skip floating point entirely and multiply in a loop:

#include <iostream>

long long intPow(int base, int exp) {
    long long result = 1;
    for (int i = 0; i < exp; ++i)
        result *= base;
    return result;
}

int main() {
    std::cout << intPow(10, 2) << "\n";  // exactly 100
    std::cout << intPow(2, 10) << "\n";  // 1024
    return 0;
}

Using long long gives room for large results before they overflow. This little function is the safest choice whenever the answer must be a precise whole number.


Squaring and Cubing

To square a number, the clearest and fastest approach is to just multiply it by itself — no pow needed:

#include <iostream>
#include <cmath>

int main() {
    double x = 5.0;
    std::cout << x * x << "\n";                 // 25 — simplest square
    std::cout << std::round(std::pow(3, 4));    // 81, rounded to be safe
    std::cout << "\n";
    return 0;
}

For small fixed powers like squares and cubes, plain multiplication is both faster and exact. Save pow for when the exponent is a variable or not a whole number.


Quick Reference

GoalCode
Raise to a powerstd::pow(base, exp)
Square a numberx * x
Exact integer powercustom intPow loop
Safe integer from powstd::round(std::pow(...))
What ^ actually doesbitwise XOR, not power


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Even or Odd Program: Check Any Number (3 Ways)
Next Post
How to Find an Element in a Vector in C++ (std::find)