base-4.8.2.0: Basic libraries

Data.Bits

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 Data.Int and Data.Word modules.

Synopsis

# Documentation

class Eq a => Bits a where Source

The Bits class defines bitwise operations over integral types.

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

Minimal complete definition

Methods

(.&.) :: a -> a -> a infixl 7 Source

Bitwise "and"

(.|.) :: a -> a -> a infixl 5 Source

Bitwise "or"

xor :: a -> a -> a infixl 6 Source

Bitwise "xor"

complement :: a -> a Source

Reverse all the bits in the argument

shift :: a -> Int -> a infixl 8 Source

shift x i shifts x left by i bits if i is positive, or right by -i bits otherwise. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

An instance can define either this unified shift or shiftL and shiftR, depending on which is more convenient for the type in question.

rotate :: a -> Int -> a infixl 8 Source

rotate x i rotates x left by i bits if i is positive, or right by -i bits otherwise.

For unbounded types like Integer, rotate is equivalent to shift.

An instance can define either this unified rotate or rotateL and rotateR, depending on which is more convenient for the type in question.

zeroBits :: a Source

zeroBits is the value with all bits unset.

The following laws ought to hold (for all valid bit indices n):

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

bit :: Int -> a Source

bit i is a value with the ith bit set and all other bits clear.

Can be implemented using bitDefault if a is also an instance of Num.

setBit :: a -> Int -> a Source

x `setBit` i is the same as x .|. bit i

clearBit :: a -> Int -> a Source

x `clearBit` i is the same as x .&. complement (bit i)

complementBit :: a -> Int -> a Source

x `complementBit` i is the same as x `xor` bit i

testBit :: a -> Int -> Bool Source

Return True if the nth bit of the argument is 1

Can be implemented using testBitDefault if a is also an instance of Num.

bitSizeMaybe :: a -> Maybe Int Source

Return the number of bits in the type of the argument. The actual value of the argument is ignored. Returns Nothing for types that do not have a fixed bitsize, like Integer.

Since: 4.7.0.0

bitSize :: a -> Int Source

Deprecated: Use bitSizeMaybe or finiteBitSize instead

Return the number of bits in the type of the argument. The actual value of the argument is ignored. The function bitSize is undefined for types that do not have a fixed bitsize, like Integer.

isSigned :: a -> Bool Source

Return True if the argument is a signed type. The actual value of the argument is ignored

shiftL :: a -> Int -> a infixl 8 Source

Shift the argument left by the specified number of bits (which must be non-negative).

An instance can define either this and shiftR or the unified shift, depending on which is more convenient for the type in question.

unsafeShiftL :: a -> Int -> a Source

Shift the argument left by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the bitSize.

Defaults to shiftL unless defined explicitly by an instance.

Since: 4.5.0.0

shiftR :: a -> Int -> a infixl 8 Source

Shift the first argument right by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the bitSize.

Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

An instance can define either this and shiftL or the unified shift, depending on which is more convenient for the type in question.

unsafeShiftR :: a -> Int -> a Source

Shift the first argument right by the specified number of bits, which must be non-negative an smaller than the number of bits in the type.

Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

Defaults to shiftR unless defined explicitly by an instance.

Since: 4.5.0.0

rotateL :: a -> Int -> a infixl 8 Source

Rotate the argument left by the specified number of bits (which must be non-negative).

An instance can define either this and rotateR or the unified rotate, depending on which is more convenient for the type in question.

rotateR :: a -> Int -> a infixl 8 Source

Rotate the argument right by the specified number of bits (which must be non-negative).

An instance can define either this and rotateL or the unified rotate, depending on which is more convenient for the type in question.

popCount :: a -> Int Source

Return the number of set bits in the argument. This number is known as the population count or the Hamming weight.

Can be implemented using popCountDefault if a is also an instance of Num.

Since: 4.5.0.0

Instances

 Source Methods(.&.) :: Bool -> Bool -> Bool Source(.|.) :: Bool -> Bool -> Bool Sourcexor :: Bool -> Bool -> Bool Sourceshift :: Bool -> Int -> Bool Sourcerotate :: Bool -> Int -> Bool Sourcebit :: Int -> Bool SourcesetBit :: Bool -> Int -> Bool SourcetestBit :: Bool -> Int -> Bool SourceshiftL :: Bool -> Int -> Bool SourceshiftR :: Bool -> Int -> Bool SourcerotateL :: Bool -> Int -> Bool SourcerotateR :: Bool -> Int -> Bool Source Source Methods(.&.) :: Int -> Int -> Int Source(.|.) :: Int -> Int -> Int Sourcexor :: Int -> Int -> Int Sourceshift :: Int -> Int -> Int Sourcerotate :: Int -> Int -> Int Sourcebit :: Int -> Int SourcesetBit :: Int -> Int -> Int SourceclearBit :: Int -> Int -> Int SourcetestBit :: Int -> Int -> Bool SourceshiftL :: Int -> Int -> Int SourceshiftR :: Int -> Int -> Int SourcerotateL :: Int -> Int -> Int SourcerotateR :: Int -> Int -> Int Source Source Methods(.&.) :: Int8 -> Int8 -> Int8 Source(.|.) :: Int8 -> Int8 -> Int8 Sourcexor :: Int8 -> Int8 -> Int8 Sourceshift :: Int8 -> Int -> Int8 Sourcerotate :: Int8 -> Int -> Int8 Sourcebit :: Int -> Int8 SourcesetBit :: Int8 -> Int -> Int8 SourcetestBit :: Int8 -> Int -> Bool SourceshiftL :: Int8 -> Int -> Int8 SourceshiftR :: Int8 -> Int -> Int8 SourcerotateL :: Int8 -> Int -> Int8 SourcerotateR :: Int8 -> Int -> Int8 Source Source Methodsxor :: Int16 -> Int16 -> Int16 Sourceshift :: Int16 -> Int -> Int16 Source Source Methodsxor :: Int32 -> Int32 -> Int32 Sourceshift :: Int32 -> Int -> Int32 Source Source Methodsxor :: Int64 -> Int64 -> Int64 Sourceshift :: Int64 -> Int -> Int64 Source Source Methods Source Methods(.&.) :: Word -> Word -> Word Source(.|.) :: Word -> Word -> Word Sourcexor :: Word -> Word -> Word Sourceshift :: Word -> Int -> Word Sourcerotate :: Word -> Int -> Word Sourcebit :: Int -> Word SourcesetBit :: Word -> Int -> Word SourcetestBit :: Word -> Int -> Bool SourceshiftL :: Word -> Int -> Word SourceshiftR :: Word -> Int -> Word SourcerotateL :: Word -> Int -> Word SourcerotateR :: Word -> Int -> Word Source Source Methodsxor :: Word8 -> Word8 -> Word8 Sourceshift :: Word8 -> Int -> Word8 Source Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methodsxor :: CSize -> CSize -> CSize Sourceshift :: CSize -> Int -> CSize Source Source Methods Source Methods Source Methods Source Methods Source Methodsxor :: CLong -> CLong -> CLong Sourceshift :: CLong -> Int -> CLong Source Source Methodsxor :: CUInt -> CUInt -> CUInt Sourceshift :: CUInt -> Int -> CUInt Source Source Methods(.&.) :: CInt -> CInt -> CInt Source(.|.) :: CInt -> CInt -> CInt Sourcexor :: CInt -> CInt -> CInt Sourceshift :: CInt -> Int -> CInt Sourcerotate :: CInt -> Int -> CInt Sourcebit :: Int -> CInt SourcesetBit :: CInt -> Int -> CInt SourcetestBit :: CInt -> Int -> Bool SourceshiftL :: CInt -> Int -> CInt SourceshiftR :: CInt -> Int -> CInt SourcerotateL :: CInt -> Int -> CInt SourcerotateR :: CInt -> Int -> CInt Source Source Methods Source Methods Source Methods Source Methods Source Methodsxor :: CChar -> CChar -> CChar Sourceshift :: CChar -> Int -> CChar Source Source Methods Source Methods Source Methods(.&.) :: Fd -> Fd -> Fd Source(.|.) :: Fd -> Fd -> Fd Sourcexor :: Fd -> Fd -> Fd Sourceshift :: Fd -> Int -> Fd Sourcerotate :: Fd -> Int -> Fd Sourcebit :: Int -> Fd SourcesetBit :: Fd -> Int -> Fd SourceclearBit :: Fd -> Int -> Fd SourcetestBit :: Fd -> Int -> Bool SourceshiftL :: Fd -> Int -> Fd SourceshiftR :: Fd -> Int -> Fd SourcerotateL :: Fd -> Int -> Fd SourcerotateR :: Fd -> Int -> Fd Source Source Methodsxor :: CRLim -> CRLim -> CRLim Sourceshift :: CRLim -> Int -> CRLim Source Source Methods Source Methods(.&.) :: CUid -> CUid -> CUid Source(.|.) :: CUid -> CUid -> CUid Sourcexor :: CUid -> CUid -> CUid Sourceshift :: CUid -> Int -> CUid Sourcerotate :: CUid -> Int -> CUid Sourcebit :: Int -> CUid SourcesetBit :: CUid -> Int -> CUid SourcetestBit :: CUid -> Int -> Bool SourceshiftL :: CUid -> Int -> CUid SourceshiftR :: CUid -> Int -> CUid SourcerotateL :: CUid -> Int -> CUid SourcerotateR :: CUid -> Int -> CUid Source Source Methods Source Methods(.&.) :: CGid -> CGid -> CGid Source(.|.) :: CGid -> CGid -> CGid Sourcexor :: CGid -> CGid -> CGid Sourceshift :: CGid -> Int -> CGid Sourcerotate :: CGid -> Int -> CGid Sourcebit :: Int -> CGid SourcesetBit :: CGid -> Int -> CGid SourcetestBit :: CGid -> Int -> Bool SourceshiftL :: CGid -> Int -> CGid SourceshiftR :: CGid -> Int -> CGid SourcerotateL :: CGid -> Int -> CGid SourcerotateR :: CGid -> Int -> CGid Source Source Methods Source Methods(.&.) :: CPid -> CPid -> CPid Source(.|.) :: CPid -> CPid -> CPid Sourcexor :: CPid -> CPid -> CPid Sourceshift :: CPid -> Int -> CPid Sourcerotate :: CPid -> Int -> CPid Sourcebit :: Int -> CPid SourcesetBit :: CPid -> Int -> CPid SourcetestBit :: CPid -> Int -> Bool SourceshiftL :: CPid -> Int -> CPid SourceshiftR :: CPid -> Int -> CPid SourcerotateL :: CPid -> Int -> CPid SourcerotateR :: CPid -> Int -> CPid Source Source Methods(.&.) :: COff -> COff -> COff Source(.|.) :: COff -> COff -> COff Sourcexor :: COff -> COff -> COff Sourceshift :: COff -> Int -> COff Sourcerotate :: COff -> Int -> COff Sourcebit :: Int -> COff SourcesetBit :: COff -> Int -> COff SourcetestBit :: COff -> Int -> Bool SourceshiftL :: COff -> Int -> COff SourceshiftR :: COff -> Int -> COff SourcerotateL :: COff -> Int -> COff SourcerotateR :: COff -> Int -> COff Source Source Methodsxor :: CMode -> CMode -> CMode Sourceshift :: CMode -> Int -> CMode Source Source Methods(.&.) :: CIno -> CIno -> CIno Source(.|.) :: CIno -> CIno -> CIno Sourcexor :: CIno -> CIno -> CIno Sourceshift :: CIno -> Int -> CIno Sourcerotate :: CIno -> Int -> CIno Sourcebit :: Int -> CIno SourcesetBit :: CIno -> Int -> CIno SourcetestBit :: CIno -> Int -> Bool SourceshiftL :: CIno -> Int -> CIno SourceshiftR :: CIno -> Int -> CIno SourcerotateL :: CIno -> Int -> CIno SourcerotateR :: CIno -> Int -> CIno Source Source Methods(.&.) :: CDev -> CDev -> CDev Source(.|.) :: CDev -> CDev -> CDev Sourcexor :: CDev -> CDev -> CDev Sourceshift :: CDev -> Int -> CDev Sourcerotate :: CDev -> Int -> CDev Sourcebit :: Int -> CDev SourcesetBit :: CDev -> Int -> CDev SourcetestBit :: CDev -> Int -> Bool SourceshiftL :: CDev -> Int -> CDev SourceshiftR :: CDev -> Int -> CDev SourcerotateL :: CDev -> Int -> CDev SourcerotateR :: CDev -> Int -> CDev Source Source Methods

class Bits b => FiniteBits b where Source

The FiniteBits class denotes types with a finite, fixed number of bits.

Since: 4.7.0.0

Minimal complete definition

finiteBitSize

Methods

finiteBitSize :: b -> Int Source

Return the number of bits in the type of the argument. The actual value of the argument is ignored. Moreover, finiteBitSize is total, in contrast to the deprecated bitSize function it replaces.

finiteBitSize = bitSize
bitSizeMaybe = Just . finiteBitSize

Since: 4.7.0.0

countLeadingZeros :: b -> Int Source

Count number of zero bits preceding the most significant set bit.

countLeadingZeros (zeroBits :: a) = finiteBitSize (zeroBits :: a)

countLeadingZeros can be used to compute log base 2 via

logBase2 x = finiteBitSize x - 1 - countLeadingZeros x

Note: The default implementation for this method is intentionally naive. However, the instances provided for the primitive integral types are implemented using CPU specific machine instructions.

Since: 4.8.0.0

countTrailingZeros :: b -> Int Source

Count number of zero bits following the least significant set bit.

countTrailingZeros (zeroBits :: a) = finiteBitSize (zeroBits :: a)
countTrailingZeros . negate = countTrailingZeros

The related find-first-set operation can be expressed in terms of countTrailingZeros as follows

findFirstSet x = 1 + countTrailingZeros x

Note: The default implementation for this method is intentionally naive. However, the instances provided for the primitive integral types are implemented using CPU specific machine instructions.

Since: 4.8.0.0

Instances

 Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods Source Methods

bitDefault :: (Bits a, Num a) => Int -> a Source

Default implementation for bit.

Note that: bitDefault i = 1 shiftL i

Since: 4.6.0.0

testBitDefault :: (Bits a, Num a) => a -> Int -> Bool Source

Default implementation for testBit.

Note that: testBitDefault x i = (x .&. bit i) /= 0

Since: 4.6.0.0

popCountDefault :: (Bits a, Num a) => a -> Int Source

Default implementation for popCount.

This implementation is intentionally naive. Instances are expected to provide an optimized implementation for their size.

Since: 4.6.0.0

toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b Source

Attempt to convert an Integral type a to an Integral type b using the size of the types as measured by Bits methods.

A simpler version of this function is:

toIntegral :: (Integral a, Integral b) => a -> Maybe b
toIntegral x
| toInteger x == y = Just (fromInteger y)
| otherwise        = Nothing
where
y = toInteger x

This version requires going through Integer, which can be inefficient. However, toIntegralSized is optimized to allow GHC to statically determine the relative type sizes (as measured by bitSizeMaybe and isSigned) and avoid going through Integer for many types. (The implementation uses fromIntegral, which is itself optimized with rules for base types but may go through Integer for some type pairs.)

Since: 4.8.0.0