统计bit 1的个数

int hammingWeight(uint32_t n)
{
    int res = 0;
    while(n)
    {
        n &= n - 1;
        ++res;
    }
    return res;
}

点赞