| Copyright | (c) Edward Kmett 2013-2014 |
|---|---|
| License | BSD3 |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Data.Bits.Extras
Description
Calculate a number of fiddly bit operations using fast de Bruijn multiplication tables.
Synopsis
- class (Num t, FiniteBits t) => Ranked t where
- log2 :: Word32 -> Int
- word32Log2 :: Word32 -> Int
- msb :: Ranked t => t -> Int
- w8 :: Integral a => a -> Word8
- w16 :: Integral a => a -> Word16
- w32 :: Integral a => a -> Word32
- w64 :: Integral a => a -> Word64
- assignBit :: Bits b => b -> Int -> Bool -> b
- zeroBits :: Bits a => a
- oneBits :: FiniteBits b => b
- unsafeOneBits :: Bits b => b
- srl :: Bits b => b -> Int -> b
Documentation
class (Num t, FiniteBits t) => Ranked t where Source #
Minimal complete definition
Methods
Calculate the least significant set bit using a debruijn multiplication table. NB: The result of this function is undefined when given 0.
Calculate the number of trailing 0 bits.
Calculate the number of leading zeros.
word32Log2 :: Word32 -> Int Source #
zeroBits is the value with all bits unset.
The following laws ought to hold (for all valid bit indices n):
clearBitzeroBitsn ==zeroBitssetBitzeroBitsn ==bitntestBitzeroBitsn == FalsepopCountzeroBits== 0
This method uses as its default
implementation (which ought to be equivalent to clearBit (bit 0) 0zeroBits for
types which possess a 0th bit).
Since: base-4.7.0.0
oneBits :: FiniteBits b => b Source #
A more concise version of complement zeroBits.
>>>complement (zeroBits :: Word) == (oneBits :: Word)True
>>>complement (oneBits :: Word) == (zeroBits :: Word)True
Note
The constraint on oneBits is arguably too strong. However, as some types
(such as Natural) have undefined complement, this is the only safe
choice.
unsafeOneBits :: Bits b => b Source #
A version of oneBits that weakens the context from FiniteBits to
Bits. This is unsafe because there are some data types with Bits
instances that have undefined complement, such as Natural. Nevertheless,
it is sometimes useful to call this function on data types without
FiniteBits instances (e.g., Integer), so this function is provided as a
convenience.