I saw your solution when I was looking for an efficient way to do this operation - but later came up with an alternative approach:
This is much faster.
var countBits = function(n) {
var count = 0;
while (n != 0)
{
n = n & (n-1);
count++;
}
return count;;
}
I thought I would share since it might be useful to you.
I saw your solution when I was looking for an efficient way to do this operation - but later came up with an alternative approach:
This is much faster.
var countBits = function(n) {var count = 0;while (n != 0){n = n & (n-1);count++;}return count;;}I thought I would share since it might be useful to you.