c - 当我运行我的代码时,它会在终端上显示一些错误。谁能告诉我错误代表什么?我该如何调试它?
问题描述
错误:
Test isPower2(0[0x0]) failed.
Gives 1[0x1]. Should be 0[0x0]
代码:
int isPower2(int x) {
int nonNega = (x>>31);
int result = !((x & (x-1)) ^ nonNega);
return result;
}
解决方案
您isPower2(0)
返回 true (1) 但 0 不是 2 的幂。因此预期结果为 false (0)。