#5872: bug in default implementation of popCount
------------------------------+---------------------------------------------
 Reporter:  lerkok            |          Owner:                  
     Type:  bug               |         Status:  new             
 Priority:  normal            |      Component:  libraries/base  
  Version:  7.4.1             |       Keywords:                  
       Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple
  Failure:  None/Unknown      |       Testcase:                  
Blockedby:                    |       Blocking:                  
  Related:                    |  
------------------------------+---------------------------------------------
 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]
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5872>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to