base-4.11.1.0: Basic libraries

Copyright(c) The University of Glasgow 2001
LicenseBSD-style (see the file libraries/base/LICENSE)
Maintainer[email protected]
Stabilityexperimental
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

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.

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: base-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.

See also zeroBits.

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: base-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: base-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 and 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: base-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: base-4.5.0.0

Instances
Bits Bool Source #

Interpret Bool as 1-bit bit-field

Since: base-4.7.0.0

Instance details

Defined in Data.Bits

Bits Int Source #

Since: base-2.1

Instance details

Defined in Data.Bits

Bits Int8 Source #

Since: base-2.1

Instance details

Defined in GHC.Int

Bits Int16 Source #

Since: base-2.1

Instance details

Defined in GHC.Int

Bits Int32 Source #

Since: base-2.1

Instance details

Defined in GHC.Int

Bits Int64 Source #

Since: base-2.1

Instance details

Defined in GHC.Int

Bits Integer Source #

Since: base-2.1

Instance details

Defined in Data.Bits

Bits Natural Source #

Since: base-4.8.0.0

Instance details

Defined in GHC.Natural

Bits Word Source #

Since: base-2.1

Instance details

Defined in Data.Bits

Bits Word8 Source #

Since: base-2.1

Instance details

Defined in GHC.Word

Bits Word16 Source #

Since: base-2.1

Instance details

Defined in GHC.Word

Bits Word32 Source #

Since: base-2.1

Instance details

Defined in GHC.Word

Bits Word64 Source #

Since: base-2.1

Instance details

Defined in GHC.Word

Bits IntPtr Source # 
Instance details

Defined in Foreign.Ptr

Bits WordPtr Source # 
Instance details

Defined in Foreign.Ptr

Bits CUIntMax Source # 
Instance details

Defined in Foreign.C.Types

Bits CIntMax Source # 
Instance details

Defined in Foreign.C.Types

Bits CUIntPtr Source # 
Instance details

Defined in Foreign.C.Types

Bits CIntPtr Source # 
Instance details

Defined in Foreign.C.Types

Bits CSigAtomic Source # 
Instance details

Defined in Foreign.C.Types

Bits CWchar Source # 
Instance details

Defined in Foreign.C.Types

Bits CSize Source # 
Instance details

Defined in Foreign.C.Types

Bits CPtrdiff Source # 
Instance details

Defined in Foreign.C.Types

Bits CBool Source # 
Instance details

Defined in Foreign.C.Types

Bits CULLong Source # 
Instance details

Defined in Foreign.C.Types

Bits CLLong Source # 
Instance details

Defined in Foreign.C.Types

Bits CULong Source # 
Instance details

Defined in Foreign.C.Types

Bits CLong Source # 
Instance details

Defined in Foreign.C.Types

Bits CUInt Source # 
Instance details

Defined in Foreign.C.Types

Bits CInt Source # 
Instance details

Defined in Foreign.C.Types

Bits CUShort Source # 
Instance details

Defined in Foreign.C.Types

Bits CShort Source # 
Instance details

Defined in Foreign.C.Types

Bits CUChar Source # 
Instance details

Defined in Foreign.C.Types

Bits CSChar Source # 
Instance details

Defined in Foreign.C.Types

Bits CChar Source # 
Instance details

Defined in Foreign.C.Types

Bits Fd Source # 
Instance details

Defined in System.Posix.Types

Bits CKey Source # 
Instance details

Defined in System.Posix.Types

Bits CId Source # 
Instance details

Defined in System.Posix.Types

Bits CFsFilCnt Source # 
Instance details

Defined in System.Posix.Types

Bits CFsBlkCnt Source # 
Instance details

Defined in System.Posix.Types

Bits CClockId Source # 
Instance details

Defined in System.Posix.Types

Bits CBlkCnt Source # 
Instance details

Defined in System.Posix.Types

Bits CBlkSize Source # 
Instance details

Defined in System.Posix.Types

Bits CRLim Source # 
Instance details

Defined in System.Posix.Types

Bits CTcflag Source # 
Instance details

Defined in System.Posix.Types

Bits CUid Source # 
Instance details

Defined in System.Posix.Types

Bits CNlink Source # 
Instance details

Defined in System.Posix.Types

Bits CGid Source # 
Instance details

Defined in System.Posix.Types

Bits CSsize Source # 
Instance details

Defined in System.Posix.Types

Bits CPid Source # 
Instance details

Defined in System.Posix.Types

Bits COff Source # 
Instance details

Defined in System.Posix.Types

Bits CMode Source # 
Instance details

Defined in System.Posix.Types

Bits CIno Source # 
Instance details

Defined in System.Posix.Types

Bits CDev Source # 
Instance details

Defined in System.Posix.Types

Bits a => Bits (Identity a) Source # 
Instance details

Defined in Data.Functor.Identity

Bits a => Bits (Const a b) Source # 
Instance details

Defined in Data.Functor.Const

Methods

(.&.) :: Const a b -> Const a b -> Const a b Source #

(.|.) :: Const a b -> Const a b -> Const a b Source #

xor :: Const a b -> Const a b -> Const a b Source #

complement :: Const a b -> Const a b Source #

shift :: Const a b -> Int -> Const a b Source #

rotate :: Const a b -> Int -> Const a b Source #

zeroBits :: Const a b Source #

bit :: Int -> Const a b Source #

setBit :: Const a b -> Int -> Const a b Source #

clearBit :: Const a b -> Int -> Const a b Source #

complementBit :: Const a b -> Int -> Const a b Source #

testBit :: Const a b -> Int -> Bool Source #

bitSizeMaybe :: Const a b -> Maybe Int Source #

bitSize :: Const a b -> Int Source #

isSigned :: Const a b -> Bool Source #

shiftL :: Const a b -> Int -> Const a b Source #

unsafeShiftL :: Const a b -> Int -> Const a b Source #

shiftR :: Const a b -> Int -> Const a b Source #

unsafeShiftR :: Const a b -> Int -> Const a b Source #

rotateL :: Const a b -> Int -> Const a b Source #

rotateR :: Const a b -> Int -> Const a b Source #

popCount :: Const a b -> Int Source #

class Bits b => FiniteBits b where Source #

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

Since: base-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: base-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: base-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: base-4.8.0.0

Instances
FiniteBits Bool Source #

Since: base-4.7.0.0

Instance details

Defined in Data.Bits

FiniteBits Int Source #

Since: base-4.6.0.0

Instance details

Defined in Data.Bits

FiniteBits Int8 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

FiniteBits Int16 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

FiniteBits Int32 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

FiniteBits Int64 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

FiniteBits Word Source #

Since: base-4.6.0.0

Instance details

Defined in Data.Bits

FiniteBits Word8 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

FiniteBits Word16 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

FiniteBits Word32 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

FiniteBits Word64 Source #

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

FiniteBits IntPtr Source # 
Instance details

Defined in Foreign.Ptr

FiniteBits WordPtr Source # 
Instance details

Defined in Foreign.Ptr

FiniteBits CUIntMax Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CIntMax Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CUIntPtr Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CIntPtr Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CSigAtomic Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CWchar Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CSize Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CPtrdiff Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CBool Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CULLong Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CLLong Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CULong Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CLong Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CUInt Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CInt Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CUShort Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CShort Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CUChar Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CSChar Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits CChar Source # 
Instance details

Defined in Foreign.C.Types

FiniteBits Fd Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CKey Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CId Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CFsFilCnt Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CFsBlkCnt Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CClockId Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CBlkCnt Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CBlkSize Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CRLim Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CTcflag Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CUid Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CNlink Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CGid Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CSsize Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CPid Source # 
Instance details

Defined in System.Posix.Types

FiniteBits COff Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CMode Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CIno Source # 
Instance details

Defined in System.Posix.Types

FiniteBits CDev Source # 
Instance details

Defined in System.Posix.Types

FiniteBits a => FiniteBits (Identity a) Source # 
Instance details

Defined in Data.Functor.Identity

FiniteBits a => FiniteBits (Const a b) Source # 
Instance details

Defined in Data.Functor.Const

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

Default implementation for bit.

Note that: bitDefault i = 1 shiftL i

Since: base-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: base-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: base-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: base-4.8.0.0