Created
July 20, 2017 04:50
-
-
Save rmunn/bc49d32a586cdfa5bcab1c3e7b45d7ac to your computer and use it in GitHub Desktop.
Bitcount (aka popcount) implementation in F#, for 32 and 64-bit ints
This file contains 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
. Nowcount2
has the structureaabbccdd
, whereaa
contains the bit count ofAA
. I.e., ifAA
was11
,aa
will be10
. IfAA
was either01
or10
,aa
will be01
, and ifAA
was00
,aa
will also be00
. (Check it for yourself via bit math if you want).count4
now has the structurebbbbdddd
, wherebb
incount4
is equal toaa
+bb
incount2
, anddd
incount4
is equal tocc
+dd
incount2
, and so on.count8
now 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 ofcount8
must have the pattern0000nnnn
, wherennnn
can be, at most, 8 (or1000
). So the top four bits of every 8 bits ofcount8
are guaranteed to be 0.Multiplying by
0x01010101
is 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 ofcount8
are 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
count8
will 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
popcnt
instruction 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).