Doug T. - 1 year ago 100

C Question

What is the most efficient way given to raise an integer to the power of another integer in C?

`// 2^3`

pow(2,3) == 8

// 5^5

pow(5,5) == 3125

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Exponentiation by squaring.

```
int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
```

This is the standard method for doing modular exponentiation for huge numbers in asymmetric cryptography.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**