1 people like it.

Count set bits in a bigint and a BitArray

Count the number of bits in a bigint (System.Numerics.BigInteger) and a BitArray. Note that version 4 is the fastest.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let bitcount (b:bigint) =
    let mutable b = b
    let mutable count = 0
    while b <> bigint 0 do
        b <- b &&& (b-(bigint 1))
        count <- count + 1
    count
val bitcount : b:bigint -> int

Full name: Script.bitcount
val b : bigint
type bigint = System.Numerics.BigInteger

Full name: Microsoft.FSharp.Core.bigint
val mutable b : bigint
val mutable count : int
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/jH
Posted:10 years ago
Author:Samuel Bosch
Tags: bigint , bits , bit manipulation , bitarray