可能重复: 询问是否数字是2的幂是 什么?这个功能有什么作用?
n & (n-1) - 这个表达式可以在哪里使用?
n & (n-1)
c
我需要解释一下这条特定的线是如何工作的. 我知道这个函数计算1位的数量,但这条线究竟是如何清除最右边的1位?
int f(int n) { int c; for (c = 0; n != 0; ++c) n = n & (n - 1); return c; }
有人可以简单地向我解释一下或给出一些"证明"吗?
c++ algorithm bitcount
这段代码意味着什么,以及在不使用位移的情况下以其他方式实现同样的目标?
if ($n & ($n - 1))
php bit-manipulation bitwise-and
algorithm ×1
bit-manipulation ×1
bitcount ×1
bitwise-and ×1
c ×1
c++ ×1
php ×1