This looks like it works well on integers that are within the max integer size in JavaScript:
function bitCount (n) {
var bits = 0
while (n !== 0) {
bits += bitCount32(n | 0)
n /= 0x100000000
}
return bits
}
function bitCount32 (n) {
n = n - ((n >> 1) & 0x55555555)
n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24
}
I'm wondering though how to count bits generally, on any sized bit stream, efficiently, ideally without converting to string.
ArrayBuffer, is that what you mean? Get a 32-bit view on it and run your function.