haskus-binary-1.5: Haskus binary format manipulation

Safe HaskellNone
LanguageHaskell2010

Haskus.Binary.Bits

Contents

Description

Operations on bits

Synopsis

Documentation

class FiniteBits a where Source #

Type representable by a fixed amount of bits

Associated Types

type BitSize a :: Nat Source #

Number of bits

Methods

bitSize :: (Integral i, KnownNat (BitSize a)) => a -> i Source #

Number of bits (the value is ignored)

zeroBits :: a Source #

All bits set to 0

oneBits :: a Source #

All bits set to 1

countLeadingZeros :: a -> Word Source #

Count number of zero bits preceding the most significant set bit

countTrailingZeros :: a -> Word Source #

Count number of zero bits following the least significant set bit

complement :: a -> a Source #

Complement

Instances
FiniteBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Int :: Nat Source #

FiniteBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Int8 :: Nat Source #

FiniteBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Int16 :: Nat Source #

FiniteBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Int32 :: Nat Source #

FiniteBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Int64 :: Nat Source #

FiniteBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Word :: Nat Source #

FiniteBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Word8 :: Nat Source #

FiniteBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Word16 :: Nat Source #

FiniteBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Word32 :: Nat Source #

FiniteBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Finite

Associated Types

type BitSize Word64 :: Nat Source #

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

Defined in Haskus.Binary.Endianness

Associated Types

type BitSize (AsLittleEndian a) :: Nat Source #

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

Defined in Haskus.Binary.Endianness

Associated Types

type BitSize (AsBigEndian a) :: Nat Source #

(KnownNat (BitSize a), FiniteBits a, KnownNat n, Storable a) => FiniteBits (Vector n a) Source # 
Instance details

Defined in Haskus.Binary.Vector

Associated Types

type BitSize (Vector n a) :: Nat Source #

class IndexableBits a where Source #

Type whose bits are indexable

Minimal complete definition

Nothing

Methods

bit :: Word -> a Source #

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

bit :: (Num a, ShiftableBits a) => Word -> a Source #

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

setBit :: a -> Word -> a Source #

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

setBit :: Bitwise a => a -> Word -> a Source #

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

clearBit :: a -> Word -> a Source #

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

clearBit :: (FiniteBits a, Bitwise a) => a -> Word -> a Source #

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

complementBit :: a -> Word -> a Source #

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

complementBit :: Bitwise a => a -> Word -> a Source #

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

testBit :: a -> Word -> Bool Source #

Return True if the nth bit of the argument is 1

testBit :: (Bitwise a, Num a, Eq a) => a -> Word -> Bool Source #

Return True if the nth bit of the argument is 1

popCount :: a -> Word Source #

Return the number of set bits

popCount :: (Bitwise a, Num a, Eq a) => a -> Word Source #

Return the number of set bits

Instances
IndexableBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Integer Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Natural Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Index

IndexableBits Buffer Source # 
Instance details

Defined in Haskus.Binary.Buffer

IndexableBits a => IndexableBits (AsLittleEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

IndexableBits a => IndexableBits (AsBigEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

(Storable a, IndexableBits a, FiniteBits a, KnownNat (BitSize a), KnownNat n, Bitwise a) => IndexableBits (Vector n a) Source # 
Instance details

Defined in Haskus.Binary.Vector

Methods

bit :: Word -> Vector n a Source #

setBit :: Vector n a -> Word -> Vector n a Source #

clearBit :: Vector n a -> Word -> Vector n a Source #

complementBit :: Vector n a -> Word -> Vector n a Source #

testBit :: Vector n a -> Word -> Bool Source #

popCount :: Vector n a -> Word Source #

class ShiftableBits a where Source #

Bit shifts

Checked means that there is an additional test to ensure that the shift offset is valid (less than the bit count). If you are sure that the offset is valid, use the "unchecked" version which should be faster.

To shift signed numbers, see SignedShiftableBits class methods.

Minimal complete definition

shiftR, shiftL, uncheckedShiftR, uncheckedShiftL

Methods

shiftR :: a -> Word -> a Source #

Checked right shift

shiftL :: a -> Word -> a Source #

Checked left shift

uncheckedShiftR :: a -> Word -> a Source #

Unchecked right shift

uncheckedShiftL :: a -> Word -> a Source #

Unchecked left shift

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

Checked shift to the left if positive, to the right if negative

uncheckedShift :: a -> Int -> a Source #

Unchecked shift to the left if positive, to the right if negative

Instances
ShiftableBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Integer Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Natural Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

ShiftableBits a => ShiftableBits (AsLittleEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

ShiftableBits a => ShiftableBits (AsBigEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

(Storable a, ShiftableBits a, Bitwise a, FiniteBits a, KnownNat (BitSize a), KnownNat (n * BitSize a), KnownNat n) => ShiftableBits (Vector n a) Source # 
Instance details

Defined in Haskus.Binary.Vector

Methods

shiftR :: Vector n a -> Word -> Vector n a Source #

shiftL :: Vector n a -> Word -> Vector n a Source #

uncheckedShiftR :: Vector n a -> Word -> Vector n a Source #

uncheckedShiftL :: Vector n a -> Word -> Vector n a Source #

shift :: Vector n a -> Int -> Vector n a Source #

uncheckedShift :: Vector n a -> Int -> Vector n a Source #

class SignedShiftableBits a where Source #

Signed bit shifts

Signed means that the sign bit (the higher order bit): - propagates to the right during right shifts and - keeps its value during left shifts (except when all other bits are 0)

Checked means that there is an additional test to ensure that the shift offset is valid (less than the bit count). If you are sure that the offset is valid, use the "unchecked" version which should be faster.

Methods

signedShiftR :: a -> Word -> a Source #

Checked signed right shift

signedShiftL :: a -> Word -> a Source #

Checked signed left shift

uncheckedSignedShiftR :: a -> Word -> a Source #

Unchecked signed right shift

uncheckedSignedShiftL :: a -> Word -> a Source #

Unchecked signed left shift

signedShift :: a -> Int -> a Source #

Checked signed shift to the left if positive, to the right if negative

uncheckedSignedShift :: a -> Int -> a Source #

Unchecked signed shift to the left if positive, to the right if negative

Instances
SignedShiftableBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

SignedShiftableBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

SignedShiftableBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

SignedShiftableBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

SignedShiftableBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Shift

class RotatableBits a where Source #

Types whose bits can be rotated

Minimal complete definition

Nothing

Methods

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

Rotate left if positive, right if negative

rotate :: (FiniteBits a, KnownNat (BitSize a)) => a -> Int -> a Source #

Rotate left if positive, right if negative

rotateL :: a -> Word -> a Source #

Checked left bit rotation

rotateL :: (FiniteBits a, KnownNat (BitSize a)) => a -> Word -> a Source #

Checked left bit rotation

rotateR :: a -> Word -> a Source #

Checked right bit rotation

rotateR :: (FiniteBits a, KnownNat (BitSize a)) => a -> Word -> a Source #

Checked right bit rotation

uncheckedRotate :: a -> Int -> a Source #

Unchecked rotate left if positive, right if negative

uncheckedRotateL :: a -> Word -> a Source #

Unchecked left bit rotation

uncheckedRotateL :: (ShiftableBits a, FiniteBits a, KnownNat (BitSize a), Bitwise a) => a -> Word -> a Source #

Unchecked left bit rotation

uncheckedRotateR :: a -> Word -> a Source #

Unchecked right bit rotation

uncheckedRotateR :: (ShiftableBits a, FiniteBits a, KnownNat (BitSize a), Bitwise a) => a -> Word -> a Source #

Unchecked right bit rotation

Instances
RotatableBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Rotate

RotatableBits a => RotatableBits (AsLittleEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

RotatableBits a => RotatableBits (AsBigEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

(Storable a, Bits a, KnownNat n, KnownNat (n * BitSize a)) => RotatableBits (Vector n a) Source # 
Instance details

Defined in Haskus.Binary.Vector

Methods

rotate :: Vector n a -> Int -> Vector n a Source #

rotateL :: Vector n a -> Word -> Vector n a Source #

rotateR :: Vector n a -> Word -> Vector n a Source #

uncheckedRotate :: Vector n a -> Int -> Vector n a Source #

uncheckedRotateL :: Vector n a -> Word -> Vector n a Source #

uncheckedRotateR :: Vector n a -> Word -> Vector n a Source #

class Bitwise a where Source #

Bitwise bit operations

Methods

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

Bitwise "and"

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

Bitwise "or"

xor :: a -> a -> a Source #

Bitwise "xor"

Instances
Bitwise Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Methods

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

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

xor :: Int -> Int -> Int Source #

Bitwise Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Methods

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

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

xor :: Int8 -> Int8 -> Int8 Source #

Bitwise Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Integer Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Natural Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Methods

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

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

xor :: Word -> Word -> Word Source #

Bitwise Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Bitwise

Bitwise Buffer Source # 
Instance details

Defined in Haskus.Binary.Buffer

Bitwise a => Bitwise (AsLittleEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

Bitwise a => Bitwise (AsBigEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

(KnownNat n, Bitwise a, Storable a) => Bitwise (Vector n a) Source # 
Instance details

Defined in Haskus.Binary.Vector

Methods

(.&.) :: Vector n a -> Vector n a -> Vector n a Source #

(.|.) :: Vector n a -> Vector n a -> Vector n a Source #

xor :: Vector n a -> Vector n a -> Vector n a Source #

Bit reversal

class ReversableBits w where Source #

Data whose bits can be reversed

Methods

reverseBits :: w -> w Source #

Instances
ReversableBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

Methods

reverseBits :: Int -> Int Source #

ReversableBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Reverse

ReversableBits a => ReversableBits (AsLittleEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

ReversableBits a => ReversableBits (AsBigEndian a) Source # 
Instance details

Defined in Haskus.Binary.Endianness

reverseBitsGeneric :: (FiniteBits a, Integral a, ShiftableBits a, Bitwise a, KnownNat (BitSize a)) => a -> a Source #

Reverse bits in a Word

reverseLeastBits :: (ShiftableBits a, FiniteBits a, ReversableBits a, KnownNat (BitSize a)) => Word -> a -> a Source #

Reverse the n least important bits of the given value. The higher bits are set to 0.

Mask

class MaskBits a where Source #

Methods

makeMaskDyn :: Word -> a Source #

Make a mask dynamically

Instances
MaskBits Int Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

Methods

makeMaskDyn :: Word -> Int Source #

MaskBits Int8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Int16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Int32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Int64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Natural Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Word Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Word8 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Word16 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Word32 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

MaskBits Word64 Source # 
Instance details

Defined in Haskus.Binary.Bits.Mask

type Maskable n a = (MaskBits a, Bitwise a, KnownNat n) Source #

maskDyn :: (MaskBits a, Bitwise a) => Word -> a -> a Source #

Keep only the n least-significant bits of the given value

mask :: forall n a. Maskable n a => a -> a Source #

Keep only the n least-significant bits of the given value

String conversion

bitsToString :: forall a. (FiniteBits a, IndexableBits a, KnownNat (BitSize a)) => a -> String Source #

Convert bits into a string composed of '0' and '1' chars

bitsToStringN :: forall a. IndexableBits a => Word -> a -> String Source #

Convert a specified amount of bits into a string composed of '0' and '1' chars

bitsFromString :: Bits a => String -> a Source #

Convert a string of '0' and '1' chars into a word

Shift

getBitRange :: forall b. (ShiftableBits b, ReversableBits b, FiniteBits b, KnownNat (BitSize b), Bitwise b, MaskBits b) => BitOrder -> Word -> Word -> b -> b Source #

`getBitRange bo offset n c` takes n bits at offset in c and put them in the least-significant bits of the result

Various

bitOffset :: Word -> Word Source #

Compute bit offset (equivalent to x mod 8 but faster)

byteOffset :: Word -> Word Source #

Compute byte offset (equivalent to x div 8 but faster)

isPowerOfTwo :: IndexableBits a => a -> Bool Source #

Check if a number is a power of two (2^n)

>>> isPowerOfTwo (10 :: Word)
False
>>> isPowerOfTwo (16 :: Word)
True

isPowerOfFour :: (IndexableBits a, FiniteBits a) => a -> Bool Source #

Check if a number is a power of four (4^n)

>>> isPowerOfFour (10 :: Word)
False
>>> isPowerOfFour (16 :: Word)
True

getPowerOfTwo :: (IndexableBits a, FiniteBits a) => a -> Maybe Word Source #

Check if a number is a power of two (2^n) and return n

>>> getPowerOfTwo (10 :: Word)
Nothing
>>> getPowerOfTwo (16 :: Word)
Just 4

getPowerOfFour :: (IndexableBits a, FiniteBits a) => a -> Maybe Word Source #

Check if a number is a power of four (4^n) and return n

>>> getPowerOfFour (10 :: Word)
Nothing
>>> getPowerOfFour (16 :: Word)
Just 2