Skip to content

Commit

Permalink
Add new Data.Bits.Bits(zeroBits) method
Browse files Browse the repository at this point in the history
This adds a new method to `Bits` which completes the Bits API
with a direct way to introduce a value with all bits cleared.

See also original proposal at

  http://permalink.gmane.org/gmane.comp.lang.haskell.libraries/21157
  • Loading branch information
hvr committed Feb 28, 2014
1 parent c7e0ea3 commit 83bd2f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Data/Bits.hs
Expand Up @@ -25,6 +25,7 @@ module Data.Bits (
complement,
shift,
rotate,
zeroBits,
bit,
setBit,
clearBit,
Expand Down Expand Up @@ -131,7 +132,26 @@ class Eq a => Bits a where
| i>0 = (x `shift` i) .|. (x `shift` (i-bitSize x))
-}

-- | @bit i@ is a value with the @i@th bit set and all other bits clear
-- | 'zeroBits' is the value with all bits unset.
--
-- The following laws ought to hold (for all valid bit indices @/n/@):
--
-- * @'clearBit' 'zeroBits' /n/ == 'zeroBits'@
-- * @'setBit' 'zeroBits' /n/ == 'bit' /n/@
-- * @'testBit' 'zeroBits' /n/ == False@
-- * @'popCount' 'zeroBits' == 0@
--
-- This method uses @'clearBit' ('bit' 0) 0@ as its default
-- implementation (which ought to be equivalent to 'zeroBits' for
-- types which possess a 0th bit).
--
-- /Since: 4.7.0.0/
zeroBits :: a
zeroBits = clearBit (bit 0) 0

-- | @bit /i/@ is a value with the @/i/@th bit set and all other bits clear.
--
-- See also 'zeroBits'.
bit :: Int -> a

-- | @x \`setBit\` i@ is the same as @x .|. bit i@
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Expand Up @@ -24,6 +24,9 @@
with a `bitSizeMaybe` method to replace the now obsolete
`bitsize` method.

* `Data.Bits.Bits` gained a new `zeroBits` method which completes the
`Bits` API with a direct way to introduce a value with all bits cleared.

* There are now `Bits` and `FiniteBits` instances for `Bool`.

* There are now `Eq`, `Ord`, `Show` and `Read` instances for `ZipList`.
Expand Down

0 comments on commit 83bd2f5

Please sign in to comment.