Skip to content

bug in default implementation of popCount

base 4.5.0.0 (http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/src/Data-Bits.html#popCount) gives the default implementation of popCount of the Bits class like this:

popCount          :: a -> Int
popCount = go 0
    where
        go !c 0 = c
        go c w = go (c+1) (w .&. w - 1)

Alas, .&. binds more tightly than -, thus the last expression parses as ((w .&. w) - 1) not as (w .&. (w-1)), as it was intended.

At the least, this causes popCount :: Integer -> Int to behave erratically:

Prelude Data.Bits> map popCount [2::Integer .. 4]
[2, 3, 4]
Trac metadata
Trac field Value
Version 7.4.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information