2022-08-12 14:59:08 +01:00
|
|
|
#include <iostream>
|
2022-08-15 15:08:54 +01:00
|
|
|
#include <cstdint> // for std::int64_t
|
|
|
|
#include <cassert> // for assert
|
2022-08-13 00:34:25 +01:00
|
|
|
|
2022-08-15 15:08:54 +01:00
|
|
|
std::int64_t powint(std::int64_t base, int exp)
|
2022-08-15 12:48:53 +01:00
|
|
|
{
|
2022-08-15 15:08:54 +01:00
|
|
|
assert(exp >= 0 && "powint: exp parameter has negative value");
|
2022-08-15 12:48:53 +01:00
|
|
|
|
2022-08-15 15:08:54 +01:00
|
|
|
std::int64_t result{ 1 };
|
|
|
|
while (exp)
|
|
|
|
{
|
|
|
|
if (exp & 1) {
|
|
|
|
std::cout << "a : " << result << "\n";
|
|
|
|
result *= base;
|
|
|
|
}
|
|
|
|
std::cout << "b : " << exp << "\n";
|
|
|
|
exp >>= 1;
|
|
|
|
std::cout << "c : " << base << "\n";
|
|
|
|
base *= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-08-15 12:48:53 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 14:59:08 +01:00
|
|
|
int main()
|
|
|
|
{
|
2022-08-15 15:08:54 +01:00
|
|
|
std::cout << powint(7, 12); // 7 to the 12th power
|
2022-08-15 12:49:34 +01:00
|
|
|
|
2022-08-15 15:08:54 +01:00
|
|
|
return 0;
|
2022-08-12 14:59:08 +01:00
|
|
|
}
|