首先很容易理解下面三个结论:
&1 的结果就是取二进制的最末位
x&1 == 0,x为偶
x&1 ==1, x为奇
#include<iostream>
using namespace std;
unsigned long long int poww(int a, int b)
{
unsigned long long int ans = 1;
unsigned long long int base = a;
while (b != 0)
{
if (b & 1 == 1)
{
ans *= base;
}
base *= base;
b >>= 1;
}
return ans;
}
int main()
{
int a, b;
cin >> a >> b;
cout << poww(a, b) <<endl;
return 0;
}