我有对整数经营以下幂函数,它工作正常:无符号长long int类型战俘
int ipow(int base, int exp)
{
int result = 1;
while(exp)
{
if (exp & 1)
{
result *= base;
}
exp >>= 1;
base *= base;
}
return result;
}
现在,我想有一个版本,它允许EXP> 32,于是我就用unsigned long类型长整型:
unsigned long long int ipow(int base, int exp)
{
unsigned long long int result = 1ULL;
while(exp)
{
if (exp & 1)
{
result *= (unsigned long long int)base;
}
exp >>= 1;
base *= base;
}
return result;
}
但是这第二个版本似乎不工作:
unsigned long long int x;
x = ipow(2, 35);
printf("%llu\n", x);
这将输出0
我的unsigned long long int实现有什么问题?
确实,非常感谢,现在就工作! – DanielFetchinson 2013-03-05 13:53:13