Created
July 20, 2017 04:50
Bitcount (aka popcount) implementation in F#, for 32 and 64-bit ints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let bitcount (n : int) = | |
| let count2 = n - ((n >>> 1) &&& 0x55555555) | |
| let count4 = (count2 &&& 0x33333333) + ((count2 >>> 2) &&& 0x33333333) | |
| let count8 = (count4 + (count4 >>> 4)) &&& 0x0f0f0f0f | |
| (count8 * 0x01010101) >>> 24 | |
| let bitcount64 (n : int64) = | |
| let count2 = n - ((n >>> 1) &&& 0x5555555555555555L) | |
| let count4 = (count2 &&& 0x3333333333333333L) + ((count2 >>> 2) &&& 0x3333333333333333L) | |
| let count8 = (count4 + (count4 >>> 4)) &&& 0x0f0f0f0f0f0f0f0fL | |
| (count8 * 0x0101010101010101L) >>> 56 |> int | |
| bitcount -1 // Result: 32 | |
| bitcount64 (-1L) // Result: 64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The way this works is:
AABBCCDD. Nowcount2has the structureaabbccdd, whereaacontains the bit count ofAA. I.e., ifAAwas11,aawill be10. IfAAwas either01or10,aawill be01, and ifAAwas00,aawill also be00. (Check it for yourself via bit math if you want).count4now has the structurebbbbdddd, wherebbincount4is equal toaa+bbincount2, andddincount4is equal tocc+ddincount2, and so on.count8now has, every 8 bits, the bitcount of the corresponding 8 bits from the original number. (And since the maximum number of bits set in 8 bits is, of course, 8, that means that every 8 bits ofcount8must have the pattern0000nnnn, wherennnncan be, at most, 8 (or1000). So the top four bits of every 8 bits ofcount8are guaranteed to be 0.Multiplying by
0x01010101is just a clever, and more efficient, way of doingcount8 <<< 0 + count8 <<< 8 + count8 <<< 16 + count8 <<< 24. The top 8 bits of that number end up being the sum of all those 8-bit values, and there's no danger of bit overflow interfering because the top 4 bits of every 8-bit segment ofcount8are guaranteed to be 0.The 64-bit function works exactly the same way, except that we only guarantee that the top three bits of every 8 bits of
count8will be 0. That's still enough to ensure no overflow in the final multiplication step.If you have direct access to the processor, the CPU's
popcntinstruction is the best way to go, but that's not available in F# (or in C#), so this is the next best approach.Source: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel (which notes that this algorithm is in the public domain).