A neat one for large, sparse integers is:
for(i=0; !x; ++i) {
x = (x-1)&x
}
return i;
Which runs only a number of iterations equal to the number of 1 bits. This works because (x-1) actually just flips the rightmost 1 bit and all zeroes to its right, then the & zeroes all of those.It's not that fast unless your integer is really sparse (since it has a branch), but I've always liked the bit hack.