packages feed

bitwise-0.2: base40/Compat.hs

module Compat
  ( unsafeIOToST
  , uncons
  , packCStringLen
  , unsafeUseAsCStringLen
  , popCount
  , zeroBits
  ) where

import Control.Monad.ST (unsafeIOToST)
import Data.ByteString (uncons, packCStringLen)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.Bits (Bits, (.&.), shiftR, clearBit, bit)

popCount :: Word64 -> Int
popCount = fromIntegral . sum . map (.&. 1) . take 64 . iterate (`shiftR` 1)

zeroBits :: Bits b => b
zeroBits = clearBit (bit 0) 0