HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerebase-1.21.2Haskell2010

Rebase.Data.Bits

  • 4 types
  • 2 classes
  • 10 values
  • Packagerebase-1.21.2
  • Exports16
  • LanguageHaskell2010
  • LicenceMIT
  • SourceBits.hs
classclass Eq 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 -> ainfixl 7

    Bitwise "and"

  • (.|.) :: a -> a -> ainfixl 5

    Bitwise "or"

  • xor :: a -> a -> ainfixl 6

    Bitwise "xor"

  • complement :: a -> a

    Reverse all the bits in the argument

  • shift :: a -> Int -> ainfixl 8

    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 -> ainfixl 8

    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

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

  • bit :: Int -> a

    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

    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

    x `testBit` i is the same as x .&. bit n /= 0

    In other words it returns True if the bit at offset @n is set.

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

  • bitSizeMaybe :: a -> Maybe Int

    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.

  • bitSize :: a -> Int

    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

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

  • shiftL :: a -> Int -> ainfixl 8

    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

    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.

  • shiftR :: a -> Int -> ainfixl 8

    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

    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.

  • rotateL :: a -> Int -> ainfixl 8

    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 -> ainfixl 8

    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

    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.

Instances66Bits, …
  • Bits IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits EventTypeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPoll
  • Bits EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Bits CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits

    Interpret Bool as 1-bit bit-field

  • Bits IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits a => Bits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Bits a => Bits (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • Bits a => Bits (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
  • Bits a => Bits (Tagged s a)Defined in tagged-0.8.9 · Data.Tagged
classclass Bits b => FiniteBits b where
#

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

Methods

Instances64FiniteBits, …
valuepopCountDefault :: (Bits a, Num a) => a -> Int
#

Default implementation for popCount.

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

valuetoIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
#

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 == toInteger y = Just y
  | otherwise                  = Nothing
  where
    y = fromIntegral 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.)

value(.^.) :: Bits a => a -> a -> a
#

Infix version of xor.

valueoneBits :: FiniteBits a => a
#

A more concise version of complement zeroBits.

Example1 expression
complement (zeroBits :: Word) == (oneBits :: Word)True
Example1 expression
complement (oneBits :: Word) == (zeroBits :: Word)True

Note

The constraint on oneBits is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

newtypenewtype And a
#

Monoid under bitwise AND.

Example1 expression
getAnd (And 0xab <> And 0x12) :: Word82

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => Monoid (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • Bits a => Bits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Iff a
#

Monoid under bitwise 'equality'; defined as 1 if the corresponding bits match, and 0 otherwise.

Example1 expression
getIff (Iff 0xab <> Iff 0x12) :: Word870

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => Semigroup (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • FiniteBits a => Monoid (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • Bits a => Bits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Ior a
#

Monoid under bitwise inclusive OR.

Example1 expression
getIor (Ior 0xab <> Ior 0x12) :: Word8187

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Monoid (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Xor a
#

Monoid under bitwise XOR.

Example1 expression
getXor (Xor 0xab <> Xor 0x12) :: Word8185

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Monoid (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits