foundation-0.0.30: Alternative prelude with batteries and no dependencies
Safe HaskellNone
LanguageHaskell2010

Foundation.Bits

Synopsis

Documentation

(.<<.) :: Bits a => a -> Int -> a #

Unsafe Shift Left Operator

(.>>.) :: Bits a => a -> Int -> a #

Unsafe Shift Right Operator

class 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 -> a infixl 7 #

Bitwise "and"

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

Bitwise "or"

xor :: a -> a -> a infixl 6 #

Bitwise "xor"

complement :: a -> a #

Reverse all the bits in the argument

shift :: a -> Int -> a infixl 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 -> a infixl 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).

@since base-4.7.0.0

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.

@since base-4.7.0.0

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 -> a infixl 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.

@since base-4.5.0.0

shiftR :: a -> Int -> a infixl 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.

@since base-4.5.0.0

rotateL :: a -> Int -> a infixl 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 -> a infixl 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.

@since base-4.5.0.0

Instances

Instances details
Bits Word128 
Instance details

Defined in Basement.Types.Word128

Bits Word256 
Instance details

Defined in Basement.Types.Word256

Bits CBool 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CChar 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CInt 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CIntMax 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CIntPtr 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CLLong 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CLong 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CPtrdiff 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CSChar 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CShort 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CSigAtomic 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CSize 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CUChar 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CUInt 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CUIntMax 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CUIntPtr 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CULLong 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CULong 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CUShort 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits CWchar 
Instance details

Defined in GHC.Internal.Foreign.C.Types

Bits Int16

@since base-2.01

Instance details

Defined in GHC.Internal.Int

Bits Int32

@since base-2.01

Instance details

Defined in GHC.Internal.Int

Bits Int64

@since base-2.01

Instance details

Defined in GHC.Internal.Int

Bits Int8

@since base-2.01

Instance details

Defined in GHC.Internal.Int

Bits CBlkCnt 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CBlkSize 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CClockId 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CDev 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CFsBlkCnt 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CFsFilCnt 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CGid 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CId 
Instance details

Defined in GHC.Internal.System.Posix.Types

Methods

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

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

xor :: CId -> CId -> CId #

complement :: CId -> CId #

shift :: CId -> Int -> CId #

rotate :: CId -> Int -> CId #

zeroBits :: CId #

bit :: Int -> CId #

setBit :: CId -> Int -> CId #

clearBit :: CId -> Int -> CId #

complementBit :: CId -> Int -> CId #

testBit :: CId -> Int -> Bool #

bitSizeMaybe :: CId -> Maybe Int #

bitSize :: CId -> Int #

isSigned :: CId -> Bool #

shiftL :: CId -> Int -> CId #

unsafeShiftL :: CId -> Int -> CId #

shiftR :: CId -> Int -> CId #

unsafeShiftR :: CId -> Int -> CId #

rotateL :: CId -> Int -> CId #

rotateR :: CId -> Int -> CId #

popCount :: CId -> Int #

Bits CIno 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CKey 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CMode 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CNfds 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CNlink 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits COff 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CPid 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CRLim 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CSocklen 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CSsize 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CTcflag 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits CUid 
Instance details

Defined in GHC.Internal.System.Posix.Types

Bits Fd 
Instance details

Defined in GHC.Internal.System.Posix.Types

Methods

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

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

xor :: Fd -> Fd -> Fd #

complement :: Fd -> Fd #

shift :: Fd -> Int -> Fd #

rotate :: Fd -> Int -> Fd #

zeroBits :: Fd #

bit :: Int -> Fd #

setBit :: Fd -> Int -> Fd #

clearBit :: Fd -> Int -> Fd #

complementBit :: Fd -> Int -> Fd #

testBit :: Fd -> Int -> Bool #

bitSizeMaybe :: Fd -> Maybe Int #

bitSize :: Fd -> Int #

isSigned :: Fd -> Bool #

shiftL :: Fd -> Int -> Fd #

unsafeShiftL :: Fd -> Int -> Fd #

shiftR :: Fd -> Int -> Fd #

unsafeShiftR :: Fd -> Int -> Fd #

rotateL :: Fd -> Int -> Fd #

rotateR :: Fd -> Int -> Fd #

popCount :: Fd -> Int #

Bits Word16

@since base-2.01

Instance details

Defined in GHC.Internal.Word

Bits Word32

@since base-2.01

Instance details

Defined in GHC.Internal.Word

Bits Word64

@since base-2.01

Instance details

Defined in GHC.Internal.Word

Bits Word8

@since base-2.01

Instance details

Defined in GHC.Internal.Word

Bits Integer

@since base-2.01

Instance details

Defined in GHC.Internal.Bits

Bits Natural

@since base-4.8.0

Instance details

Defined in GHC.Internal.Bits

Bits Bool

Interpret Bool as 1-bit bit-field

@since base-4.7.0.0

Instance details

Defined in GHC.Internal.Bits

Bits Int

@since base-2.01

Instance details

Defined in GHC.Internal.Bits

Methods

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

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

xor :: Int -> Int -> Int #

complement :: Int -> Int #

shift :: Int -> Int -> Int #

rotate :: Int -> Int -> Int #

zeroBits :: Int #

bit :: Int -> Int #

setBit :: Int -> Int -> Int #

clearBit :: Int -> Int -> Int #

complementBit :: Int -> Int -> Int #

testBit :: Int -> Int -> Bool #

bitSizeMaybe :: Int -> Maybe Int #

bitSize :: Int -> Int #

isSigned :: Int -> Bool #

shiftL :: Int -> Int -> Int #

unsafeShiftL :: Int -> Int -> Int #

shiftR :: Int -> Int -> Int #

unsafeShiftR :: Int -> Int -> Int #

rotateL :: Int -> Int -> Int #

rotateR :: Int -> Int -> Int #

popCount :: Int -> Int #

Bits Word

@since base-2.01

Instance details

Defined in GHC.Internal.Bits

Bits a => Bits (BE a) 
Instance details

Defined in Basement.Endianness

Methods

(.&.) :: BE a -> BE a -> BE a #

(.|.) :: BE a -> BE a -> BE a #

xor :: BE a -> BE a -> BE a #

complement :: BE a -> BE a #

shift :: BE a -> Int -> BE a #

rotate :: BE a -> Int -> BE a #

zeroBits :: BE a #

bit :: Int -> BE a #

setBit :: BE a -> Int -> BE a #

clearBit :: BE a -> Int -> BE a #

complementBit :: BE a -> Int -> BE a #

testBit :: BE a -> Int -> Bool #

bitSizeMaybe :: BE a -> Maybe Int #

bitSize :: BE a -> Int #

isSigned :: BE a -> Bool #

shiftL :: BE a -> Int -> BE a #

unsafeShiftL :: BE a -> Int -> BE a #

shiftR :: BE a -> Int -> BE a #

unsafeShiftR :: BE a -> Int -> BE a #

rotateL :: BE a -> Int -> BE a #

rotateR :: BE a -> Int -> BE a #

popCount :: BE a -> Int #

Bits a => Bits (LE a) 
Instance details

Defined in Basement.Endianness

Methods

(.&.) :: LE a -> LE a -> LE a #

(.|.) :: LE a -> LE a -> LE a #

xor :: LE a -> LE a -> LE a #

complement :: LE a -> LE a #

shift :: LE a -> Int -> LE a #

rotate :: LE a -> Int -> LE a #

zeroBits :: LE a #

bit :: Int -> LE a #

setBit :: LE a -> Int -> LE a #

clearBit :: LE a -> Int -> LE a #

complementBit :: LE a -> Int -> LE a #

testBit :: LE a -> Int -> Bool #

bitSizeMaybe :: LE a -> Maybe Int #

bitSize :: LE a -> Int #

isSigned :: LE a -> Bool #

shiftL :: LE a -> Int -> LE a #

unsafeShiftL :: LE a -> Int -> LE a #

shiftR :: LE a -> Int -> LE a #

unsafeShiftR :: LE a -> Int -> LE a #

rotateL :: LE a -> Int -> LE a #

rotateR :: LE a -> Int -> LE a #

popCount :: LE a -> Int #

Bits a => Bits (Identity a)

@since base-4.9.0.0

Instance details

Defined in GHC.Internal.Data.Functor.Identity

Bits a => Bits (Const a b)

@since base-4.9.0.0

Instance details

Defined in GHC.Internal.Data.Functor.Const

Methods

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

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

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

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

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

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

zeroBits :: Const a b #

bit :: Int -> Const a b #

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

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

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

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

bitSizeMaybe :: Const a b -> Maybe Int #

bitSize :: Const a b -> Int #

isSigned :: Const a b -> Bool #

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

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

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

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

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

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

popCount :: Const a b -> Int #

alignRoundUp #

Arguments

:: Int

Number to Align

-> Int

Alignment (power of 2)

-> Int 

Round up (if needed) to a multiple of alignment closst to m

alignment needs to be a power of two

alignRoundUp 16 8 = 16 alignRoundUp 15 8 = 16

alignRoundDown #

Arguments

:: Int

Number to Align

-> Int

Alignment (power of 2)

-> Int 

Round down (if needed) to a multiple of alignment closest to m

alignment needs to be a power of two

alignRoundDown 15 8 = 8
alignRoundDown 8 8  = 8