Haskell Core Libraries (base package)ParentContentsIndex
Data.Bits
Portability portable
Stability experimental
Maintainer libraries@haskell.org
Contents
The Bits class
Shifts and rotates
Description
This module defines bitwise operations for signed and unsigned integers. Instances of the class Bits for the Int and Integer types are available from this module, and instances for explicitly sized integral types are available from the Int and Word modules.
Synopsis
class (Num a) => Bits a where
isSigned :: a -> Bool
bitSize :: a -> Int
testBit :: a -> Int -> Bool
complementBit :: a -> Int -> a
clearBit :: a -> Int -> a
setBit :: a -> Int -> a
bit :: Int -> a
rotate :: a -> Int -> a
shift :: a -> Int -> a
complement :: a -> a
xor :: a -> a -> a
(.|.) :: a -> a -> a
(.&.) :: a -> a -> a
shiftL :: (Bits a) => a -> Int -> a
shiftR :: (Bits a) => a -> Int -> a
rotateL :: (Bits a) => a -> Int -> a
rotateR :: (Bits a) => a -> Int -> a
The Bits class
class (Num a) => Bits a where

The Bits class defines bitwise operations over integral types.

  • Bits are numbered from 0 with bit 0 being the least significant bit.

Methods
isSigned :: a -> Bool
bitSize :: a -> Int
Return True if the argument is a signed type. The actual value of the argument is ignored
testBit :: a -> Int -> Bool
Return the number of bits in the type of the argument. The actual value of the argument is ignored
complementBit :: a -> Int -> a
Return True if the nth bit of the argument is 1
clearBit :: a -> Int -> a
x `complementBit` i is the same as x `xor` bit i
setBit :: a -> Int -> a
x `clearBit` i is the same as x .&. complement (bit i)
bit :: Int -> a
x `setBit` i is the same as x .|. bit i
rotate :: a -> Int -> a
bit i is a value with the ith bit set
shift :: a -> Int -> a

Signed rotate the argument left by the specified number of bits. Right rotates are specified by giving a negative value.

rotate is well defined only if bitSize is also well defined (bitSize is undefined for Integer, for example).

complement :: a -> a
Signed shift the argument left by the specified number of bits. Right shifts are specified by giving a negative value.
xor :: a -> a -> a
Reverse all the bits in the argument
(.|.) :: a -> a -> a
Bitwise "xor"
(.&.) :: a -> a -> a
Bitwise "or"
Instances
Bits Int
Bits Integer
Bits Int8
Bits Int16
Bits Int32
Bits Int64
Bits Word
Bits Word8
Bits Word16
Bits Word32
Bits Word64
Shifts and rotates
These functions might sometimes be more convenient than the unified versions shift and rotate.
shiftL :: (Bits a) => a -> Int -> a
shiftR :: (Bits a) => a -> Int -> a
rotateL :: (Bits a) => a -> Int -> a
rotateR :: (Bits a) => a -> Int -> a
Produced by Haddock version 0.3