I’ll add more detail, implementations of bit counting in Luau, and comparisons later. I just couldn’t find a hypothetical function like bit32.count_bits and was kinda bored lol so here you go
local Kernighan = function(n)
local count = 0
while n > 0 do
n = bit32.band(n, n - 1)
count += 1
end
return count
end
Here’s a Luau implementation of Brian Kernighan’s algorithm for counting the number of set bits, or the number of 1s, in the bit representation of an integer.