http://blog.csdn.net/qq276592716/article/details/9240269
int popcount_1(UINT32 x)
{
x = (x & m1) + ((x >> 1) & m1);
x = (x & m2) + ((x >> 2) & m2);
x = (x & m4) + ((x >> 4) & m4);
x = (x & m8) + ((x >> 8) & m8);
x = (x & m16) + ((x >> 16) & m16);
return x;
}
int popcount_2(UINT32 x)
{
x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits
x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits
x += x >> 8; //put count of each 16 bits into their lowest 8 bits
x += x >> 16; //put count of each 32 bits into their lowest 8 bits
return x & 0x1f;
}
红色位置的两个表达式的计算结果是相同的,设(x & m1)为A, ((x >> 1) & m1)为B,则原x=A+2B,第一个计算后的x=A+B,第二个计算后的x=原X-B
所以两个表达式的结果是相同的,角度不同,形式方面有变化。