以下代码的作用:
(n>0 && ((n & (n-1)) == 0))
很简单:判断n是否为2的整数次幂
写程序计算从整数A变为整数B需要修改的二进制位数。
输入:31,14
输出:2
这个题也很简单:A和B异或然后再算结果中1的个数
代码:
int getModifyNum(const int vA, const int vB)
{
int Tmp = vA ^ vB;
int Count = 0;
while (Tmp > 0)
{
if (Tmp & 1) ++Count;
Tmp >>= 1;
}
return Count;
}