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.
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
| Goal | Code |
|---|---|
| Raise to a power | std::pow(base, exp) |
| Square a number | x * x |
| Exact integer power | custom intPow loop |
| Safe integer from pow | std::round(std::pow(...)) |
What ^ actually does | bitwise XOR, not power |
Related Articles
- C++ Math Functions — pow, sqrt, abs, and the rest of cmath
- C++ Modulo Operator (%) — the other math operator beginners miss
- C++ Bitwise Operators — what ^ really means
- C++ Type Casting Explained — converting double results to int
- C++ Variables and Data Types — double vs int precision
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.