TCPL 2-9

题目:在求对二的补码时,表达式x &= (x -1)可以删除x中最右边值为1的一个二进制位。请解释这样做的道理。用这一方法重写bitcount函数,以加快其执行速度。

int bitcount(unsigned x)
{
    int nCount = 0;
    while(x != 0)
    {
        x &= x - 1;
        nCount++;
    }
    return nCount;
}

猜你喜欢

转载自blog.csdn.net/qq_38035641/article/details/87916955
2-9
今日推荐