/* upto 32 bits */
/* remove 1's each time and count */
function shiftroop(x){
var count;
for (count=1; x; count++){
x &= x-1;
}
return count;
}
function popcount_32(x) {
var m1 = 0x55555555; //binary: 0101...
var m2 = 0x33333333; //binary: 00110011..
var m4 = 0x0f0f0f0f; //binary: 4 zeros, 4 ones ...
var m8 = 0x00ff00ff; //binary: 8 zeros, 8 ones ...
var m16 = 0x0000ffff; //binary: 16 zeros, 16 ones ...
x = (x & m1 ) + ((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 & m4 ) + ((x >> 4) & m4 ); //put count of each 8 bits into those 8 bits
x = (x & m8 ) + ((x >> 8) & m8 ); //put count of each 16 bits into those 16 bits
x = (x & m16) + ((x >> 16) & m16); //put count of each 32 bits into those 32 bits
return x;
}
===========================================