You can use LanguagPrimitives.GenericOne
and LanguagPrimitives.GenericZero
combined with inline
to acheive this.
let inline CountBitsWithRangedMask x upBound =
seq { for i = LanguagePrimitives.GenericZero to upBound do yield LanguagePrimitives.GenericOne <<< i }
|> Seq.map ((&&&) x)
|> Seq.filter ((<>) (LanguagePrimitives.GenericZero))
|> Seq.length
let test16 (x:System.Int16) = CountBitsWithRangedMask x 15
let test32 (x:System.Int32) = CountBitsWithRangedMask x 31
let test64 (x:System.Int64) = CountBitsWithRangedMask x 63
Using this we also don't need the typeConverter
parameter as the compiler does everything automatically.
This is a reasonably general method for these sorts of problems.