This is the Kerningham trick to calculate the number of bits without shifting. (Original author precedes Kerningham by a decade, but mr K popularised it).
n&(n-1)
peels off the least significant one, meaning that one should write the result back to n.
while (n) {
++counter;
n&=n-1;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…