/Haskell 8

# Data.Bits

Copyright (c) The University of Glasgow 2001 BSD-style (see the file libraries/base/LICENSE) [email protected] experimental portable Trustworthy Haskell2010

#### 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.

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`):

• `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: base-4.7.0.0

bit :: Int -> a Source

`bit i` is a value with the `i`th 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 `n`th 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`.

Default implementation based upon `bitSizeMaybe` provided since 4.12.0.0.

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). Some instances may throw an `Overflow` exception if given a negative input.

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`. Some instances may throw an `Overflow` exception if given a negative input.

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
Instances details
Bits Bool

Interpret `Bool` as 1-bit bit-field

Since: base-4.7.0.0

Instance details

Defined in Data.Bits

#### Methods

(.&.) :: Bool -> Bool -> Bool Source

(.|.) :: Bool -> Bool -> Bool Source

xor :: Bool -> Bool -> Bool Source

shift :: Bool -> Int -> Bool Source

rotate :: Bool -> Int -> Bool Source

bit :: Int -> Bool Source

setBit :: Bool -> Int -> Bool Source

testBit :: Bool -> Int -> Bool Source

shiftL :: Bool -> Int -> Bool Source

shiftR :: Bool -> Int -> Bool Source

rotateL :: Bool -> Int -> Bool Source

rotateR :: Bool -> Int -> Bool Source

Bits Int

Since: base-2.1

Instance details

Defined in Data.Bits

#### Methods

(.&.) :: Int -> Int -> Int Source

(.|.) :: Int -> Int -> Int Source

xor :: Int -> Int -> Int Source

shift :: Int -> Int -> Int Source

rotate :: Int -> Int -> Int Source

bit :: Int -> Int Source

setBit :: Int -> Int -> Int Source

clearBit :: Int -> Int -> Int Source

testBit :: Int -> Int -> Bool Source

shiftL :: Int -> Int -> Int Source

shiftR :: Int -> Int -> Int Source

rotateL :: Int -> Int -> Int Source

rotateR :: Int -> Int -> Int Source

Bits Int8

Since: base-2.1

Instance details

Defined in GHC.Int

#### Methods

(.&.) :: Int8 -> Int8 -> Int8 Source

(.|.) :: Int8 -> Int8 -> Int8 Source

xor :: Int8 -> Int8 -> Int8 Source

shift :: Int8 -> Int -> Int8 Source

rotate :: Int8 -> Int -> Int8 Source

bit :: Int -> Int8 Source

setBit :: Int8 -> Int -> Int8 Source

testBit :: Int8 -> Int -> Bool Source

shiftL :: Int8 -> Int -> Int8 Source

shiftR :: Int8 -> Int -> Int8 Source

rotateL :: Int8 -> Int -> Int8 Source

rotateR :: Int8 -> Int -> Int8 Source

Bits Int16

Since: base-2.1

Instance details

Defined in GHC.Int

#### Methods

xor :: Int16 -> Int16 -> Int16 Source

shift :: Int16 -> Int -> Int16 Source

Bits Int32

Since: base-2.1

Instance details

Defined in GHC.Int

#### Methods

xor :: Int32 -> Int32 -> Int32 Source

shift :: Int32 -> Int -> Int32 Source

Bits Int64

Since: base-2.1

Instance details

Defined in GHC.Int

#### Methods

xor :: Int64 -> Int64 -> Int64 Source

shift :: Int64 -> Int -> Int64 Source

Bits Integer

Since: base-2.1

Instance details

Defined in Data.Bits

#### Methods

Bits Natural

Since: base-4.8.0

Instance details

Defined in Data.Bits

#### Methods

Bits Word

Since: base-2.1

Instance details

Defined in Data.Bits

#### Methods

(.&.) :: Word -> Word -> Word Source

(.|.) :: Word -> Word -> Word Source

xor :: Word -> Word -> Word Source

shift :: Word -> Int -> Word Source

rotate :: Word -> Int -> Word Source

bit :: Int -> Word Source

setBit :: Word -> Int -> Word Source

testBit :: Word -> Int -> Bool Source

shiftL :: Word -> Int -> Word Source

shiftR :: Word -> Int -> Word Source

rotateL :: Word -> Int -> Word Source

rotateR :: Word -> Int -> Word Source

Bits Word8

Since: base-2.1

Instance details

Defined in GHC.Word

#### Methods

xor :: Word8 -> Word8 -> Word8 Source

shift :: Word8 -> Int -> Word8 Source

Bits Word16

Since: base-2.1

Instance details

Defined in GHC.Word

#### Methods

Bits Word32

Since: base-2.1

Instance details

Defined in GHC.Word

#### Methods

Bits Word64

Since: base-2.1

Instance details

Defined in GHC.Word

#### Methods

Bits IntPtr
Instance details

Defined in Foreign.Ptr

#### Methods

Bits WordPtr
Instance details

Defined in Foreign.Ptr

#### Methods

Bits CUIntMax
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CIntMax
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CUIntPtr
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CIntPtr
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CSigAtomic
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CWchar
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CSize
Instance details

Defined in Foreign.C.Types

#### Methods

xor :: CSize -> CSize -> CSize Source

shift :: CSize -> Int -> CSize Source

Bits CPtrdiff
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CBool
Instance details

Defined in Foreign.C.Types

#### Methods

xor :: CBool -> CBool -> CBool Source

shift :: CBool -> Int -> CBool Source

Bits CULLong
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CLLong
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CULong
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CLong
Instance details

Defined in Foreign.C.Types

#### Methods

xor :: CLong -> CLong -> CLong Source

shift :: CLong -> Int -> CLong Source

Bits CUInt
Instance details

Defined in Foreign.C.Types

#### Methods

xor :: CUInt -> CUInt -> CUInt Source

shift :: CUInt -> Int -> CUInt Source

Bits CInt
Instance details

Defined in Foreign.C.Types

#### Methods

(.&.) :: CInt -> CInt -> CInt Source

(.|.) :: CInt -> CInt -> CInt Source

xor :: CInt -> CInt -> CInt Source

shift :: CInt -> Int -> CInt Source

rotate :: CInt -> Int -> CInt Source

bit :: Int -> CInt Source

setBit :: CInt -> Int -> CInt Source

testBit :: CInt -> Int -> Bool Source

shiftL :: CInt -> Int -> CInt Source

shiftR :: CInt -> Int -> CInt Source

rotateL :: CInt -> Int -> CInt Source

rotateR :: CInt -> Int -> CInt Source

Bits CUShort
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CShort
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CUChar
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CSChar
Instance details

Defined in Foreign.C.Types

#### Methods

Bits CChar
Instance details

Defined in Foreign.C.Types

#### Methods

xor :: CChar -> CChar -> CChar Source

shift :: CChar -> Int -> CChar Source

Bits Fd
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: Fd -> Fd -> Fd Source

(.|.) :: Fd -> Fd -> Fd Source

xor :: Fd -> Fd -> Fd Source

shift :: Fd -> Int -> Fd Source

rotate :: Fd -> Int -> Fd Source

bit :: Int -> Fd Source

setBit :: Fd -> Int -> Fd Source

clearBit :: Fd -> Int -> Fd Source

testBit :: Fd -> Int -> Bool Source

shiftL :: Fd -> Int -> Fd Source

shiftR :: Fd -> Int -> Fd Source

rotateL :: Fd -> Int -> Fd Source

rotateR :: Fd -> Int -> Fd Source

Bits CKey
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CKey -> CKey -> CKey Source

(.|.) :: CKey -> CKey -> CKey Source

xor :: CKey -> CKey -> CKey Source

shift :: CKey -> Int -> CKey Source

rotate :: CKey -> Int -> CKey Source

bit :: Int -> CKey Source

setBit :: CKey -> Int -> CKey Source

testBit :: CKey -> Int -> Bool Source

shiftL :: CKey -> Int -> CKey Source

shiftR :: CKey -> Int -> CKey Source

rotateL :: CKey -> Int -> CKey Source

rotateR :: CKey -> Int -> CKey Source

Bits CId
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CId -> CId -> CId Source

(.|.) :: CId -> CId -> CId Source

xor :: CId -> CId -> CId Source

shift :: CId -> Int -> CId Source

rotate :: CId -> Int -> CId Source

bit :: Int -> CId Source

setBit :: CId -> Int -> CId Source

clearBit :: CId -> Int -> CId Source

testBit :: CId -> Int -> Bool Source

shiftL :: CId -> Int -> CId Source

shiftR :: CId -> Int -> CId Source

rotateL :: CId -> Int -> CId Source

rotateR :: CId -> Int -> CId Source

Bits CFsFilCnt
Instance details

Defined in System.Posix.Types

#### Methods

Bits CFsBlkCnt
Instance details

Defined in System.Posix.Types

#### Methods

Bits CClockId
Instance details

Defined in System.Posix.Types

#### Methods

Bits CBlkCnt
Instance details

Defined in System.Posix.Types

#### Methods

Bits CBlkSize
Instance details

Defined in System.Posix.Types

#### Methods

Bits CRLim
Instance details

Defined in System.Posix.Types

#### Methods

xor :: CRLim -> CRLim -> CRLim Source

shift :: CRLim -> Int -> CRLim Source

Bits CTcflag
Instance details

Defined in System.Posix.Types

#### Methods

Bits CUid
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CUid -> CUid -> CUid Source

(.|.) :: CUid -> CUid -> CUid Source

xor :: CUid -> CUid -> CUid Source

shift :: CUid -> Int -> CUid Source

rotate :: CUid -> Int -> CUid Source

bit :: Int -> CUid Source

setBit :: CUid -> Int -> CUid Source

testBit :: CUid -> Int -> Bool Source

shiftL :: CUid -> Int -> CUid Source

shiftR :: CUid -> Int -> CUid Source

rotateL :: CUid -> Int -> CUid Source

rotateR :: CUid -> Int -> CUid Source

Bits CNlink
Instance details

Defined in System.Posix.Types

#### Methods

Bits CGid
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CGid -> CGid -> CGid Source

(.|.) :: CGid -> CGid -> CGid Source

xor :: CGid -> CGid -> CGid Source

shift :: CGid -> Int -> CGid Source

rotate :: CGid -> Int -> CGid Source

bit :: Int -> CGid Source

setBit :: CGid -> Int -> CGid Source

testBit :: CGid -> Int -> Bool Source

shiftL :: CGid -> Int -> CGid Source

shiftR :: CGid -> Int -> CGid Source

rotateL :: CGid -> Int -> CGid Source

rotateR :: CGid -> Int -> CGid Source

Bits CSsize
Instance details

Defined in System.Posix.Types

#### Methods

Bits CPid
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CPid -> CPid -> CPid Source

(.|.) :: CPid -> CPid -> CPid Source

xor :: CPid -> CPid -> CPid Source

shift :: CPid -> Int -> CPid Source

rotate :: CPid -> Int -> CPid Source

bit :: Int -> CPid Source

setBit :: CPid -> Int -> CPid Source

testBit :: CPid -> Int -> Bool Source

shiftL :: CPid -> Int -> CPid Source

shiftR :: CPid -> Int -> CPid Source

rotateL :: CPid -> Int -> CPid Source

rotateR :: CPid -> Int -> CPid Source

Bits COff
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: COff -> COff -> COff Source

(.|.) :: COff -> COff -> COff Source

xor :: COff -> COff -> COff Source

shift :: COff -> Int -> COff Source

rotate :: COff -> Int -> COff Source

bit :: Int -> COff Source

setBit :: COff -> Int -> COff Source

testBit :: COff -> Int -> Bool Source

shiftL :: COff -> Int -> COff Source

shiftR :: COff -> Int -> COff Source

rotateL :: COff -> Int -> COff Source

rotateR :: COff -> Int -> COff Source

Bits CMode
Instance details

Defined in System.Posix.Types

#### Methods

xor :: CMode -> CMode -> CMode Source

shift :: CMode -> Int -> CMode Source

Bits CIno
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CIno -> CIno -> CIno Source

(.|.) :: CIno -> CIno -> CIno Source

xor :: CIno -> CIno -> CIno Source

shift :: CIno -> Int -> CIno Source

rotate :: CIno -> Int -> CIno Source

bit :: Int -> CIno Source

setBit :: CIno -> Int -> CIno Source

testBit :: CIno -> Int -> Bool Source

shiftL :: CIno -> Int -> CIno Source

shiftR :: CIno -> Int -> CIno Source

rotateL :: CIno -> Int -> CIno Source

rotateR :: CIno -> Int -> CIno Source

Bits CDev
Instance details

Defined in System.Posix.Types

#### Methods

(.&.) :: CDev -> CDev -> CDev Source

(.|.) :: CDev -> CDev -> CDev Source

xor :: CDev -> CDev -> CDev Source

shift :: CDev -> Int -> CDev Source

rotate :: CDev -> Int -> CDev Source

bit :: Int -> CDev Source

setBit :: CDev -> Int -> CDev Source

testBit :: CDev -> Int -> Bool Source

shiftL :: CDev -> Int -> CDev Source

shiftR :: CDev -> Int -> CDev Source

rotateL :: CDev -> Int -> CDev Source

rotateR :: CDev -> Int -> CDev Source

Bits a => Bits (Identity a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Identity

Bits a => Bits (Const a b)

Since: base-4.9.0.0

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

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
Instances details
FiniteBits Bool

Since: base-4.7.0.0

Instance details

Defined in Data.Bits

#### Methods

FiniteBits Int

Since: base-4.6.0.0

Instance details

Defined in Data.Bits

#### Methods

FiniteBits Int8

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

#### Methods

FiniteBits Int16

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

#### Methods

FiniteBits Int32

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

#### Methods

FiniteBits Int64

Since: base-4.6.0.0

Instance details

Defined in GHC.Int

#### Methods

FiniteBits Word

Since: base-4.6.0.0

Instance details

Defined in Data.Bits

#### Methods

FiniteBits Word8

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

#### Methods

FiniteBits Word16

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

#### Methods

FiniteBits Word32

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

#### Methods

FiniteBits Word64

Since: base-4.6.0.0

Instance details

Defined in GHC.Word

#### Methods

FiniteBits IntPtr
Instance details

Defined in Foreign.Ptr

#### Methods

FiniteBits WordPtr
Instance details

Defined in Foreign.Ptr

#### Methods

FiniteBits CUIntMax
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CIntMax
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CUIntPtr
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CIntPtr
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CSigAtomic
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CWchar
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CSize
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CPtrdiff
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CBool
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CULLong
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CLLong
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CULong
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CLong
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CUInt
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CInt
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CUShort
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CShort
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CUChar
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CSChar
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits CChar
Instance details

Defined in Foreign.C.Types

#### Methods

FiniteBits Fd
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CKey
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CId
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CFsFilCnt
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CFsBlkCnt
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CClockId
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CBlkCnt
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CBlkSize
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CRLim
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CTcflag
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CUid
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CNlink
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CGid
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CSsize
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CPid
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits COff
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CMode
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CIno
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits CDev
Instance details

Defined in System.Posix.Types

#### Methods

FiniteBits a => FiniteBits (Identity a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Identity

#### Methods

FiniteBits a => FiniteBits (Const a b)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Const

#### Methods

finiteBitSize :: Const a b -> Int Source

countLeadingZeros :: Const a b -> Int Source

countTrailingZeros :: Const a b -> Int Source

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

© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).
https://downloads.haskell.org/~ghc/8.8.3/docs/html/libraries/base-4.13.0.0/Data-Bits.html