From other searches, I found that this problem is called 'Hamming Weight' or 'Population Count'. There are lot of answers out there given with so many statistics? I need to find the solution in a simple way? Complexity is not a big deal. Is there any in-built function in JavaScript like Java's Integer.bitCount?
I'm currently doing this as follows.
var binary = 3;
var original = binary;
var count = 0;
while(binary>0)
{
binary = binary >> 1 << 1;
if(original-binary==1)
count++;
original = binary >> 1;
binary = original;
}
Is there a better, more simple as well as elegant way for this?