Join Nostr
2024-11-28 04:52:44 UTC
in reply to

Per Vognsen on Nostr: nprofile1q…6gyrf I'm invariant-pilled but I think I slightly prefer to think of ...

I'm invariant-pilled but I think I slightly prefer to think of this algorithm in terms of algebraic laws:

popcount(0) = 0
popcount(x) = popcount(clear_lowest_bit(x)) + 1 if x != 0

This can be turned directly into a function definition. Of course you have also have to prove clear_lowest_bit(x) = x & (x - 1) if x != 0.