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
(.&.) :: a -> a -> a
(.|.) :: a -> a -> a
xor :: a -> a -> a
complement :: a -> a
shift :: a -> Int -> a
rotate :: a -> Int -> a
bit :: Int -> a
setBit :: a -> Int -> a
clearBit :: a -> Int -> a
complementBit :: a -> Int -> a
testBit :: a -> Int -> Bool
bitSize :: a -> Int
isSigned :: a -> Bool
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
(.&.) :: a -> a -> a
Bitwise "and"
(.|.) :: a -> a -> a
Bitwise "or"
xor :: a -> a -> a
Bitwise "xor"
complement :: a -> a
Reverse all the bits in the argument
shift :: a -> Int -> a
Signed shift the argument left by the specified number of bits. Right shifts are specified by giving a negative value.
rotate :: 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).

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