Safe Haskell | None |
---|---|
Language | Haskell2010 |
Operations on bits
Synopsis
- type Bits a = (Eq a, FiniteBits a, IndexableBits a, ShiftableBits a, Bitwise a, RotatableBits a, KnownNat (BitSize a), MaskBits a)
- class FiniteBits a where
- type BitSize a :: Nat
- bitSize :: (Integral i, KnownNat (BitSize a)) => a -> i
- zeroBits :: a
- oneBits :: a
- countLeadingZeros :: a -> Word
- countTrailingZeros :: a -> Word
- complement :: a -> a
- class IndexableBits a where
- class ShiftableBits a where
- shiftR :: a -> Word -> a
- shiftL :: a -> Word -> a
- uncheckedShiftR :: a -> Word -> a
- uncheckedShiftL :: a -> Word -> a
- shift :: a -> Int -> a
- uncheckedShift :: a -> Int -> a
- class SignedShiftableBits a where
- signedShiftR :: a -> Word -> a
- signedShiftL :: a -> Word -> a
- uncheckedSignedShiftR :: a -> Word -> a
- uncheckedSignedShiftL :: a -> Word -> a
- signedShift :: a -> Int -> a
- uncheckedSignedShift :: a -> Int -> a
- class RotatableBits a where
- rotate :: a -> Int -> a
- rotateL :: a -> Word -> a
- rotateR :: a -> Word -> a
- uncheckedRotate :: a -> Int -> a
- uncheckedRotateL :: a -> Word -> a
- uncheckedRotateR :: a -> Word -> a
- class Bitwise a where
- class ReversableBits w where
- reverseBits :: w -> w
- reverseBitsGeneric :: (FiniteBits a, Integral a, ShiftableBits a, Bitwise a, KnownNat (BitSize a)) => a -> a
- reverseLeastBits :: (ShiftableBits a, FiniteBits a, ReversableBits a, KnownNat (BitSize a)) => Word -> a -> a
- class MaskBits a where
- makeMaskDyn :: Word -> a
- type Maskable n a = (MaskBits a, Bitwise a, KnownNat n)
- maskDyn :: (MaskBits a, Bitwise a) => Word -> a -> a
- mask :: forall n a. Maskable n a => a -> a
- bitsToString :: forall a. (FiniteBits a, IndexableBits a, KnownNat (BitSize a)) => a -> String
- bitsToStringN :: forall a. IndexableBits a => Word -> a -> String
- bitsFromString :: Bits a => String -> a
- getBitRange :: forall b. (ShiftableBits b, ReversableBits b, FiniteBits b, KnownNat (BitSize b), Bitwise b, MaskBits b) => BitOrder -> Word -> Word -> b -> b
- bitOffset :: Word -> Word
- byteOffset :: Word -> Word
- isPowerOfTwo :: IndexableBits a => a -> Bool
- isPowerOfFour :: (IndexableBits a, FiniteBits a) => a -> Bool
- getPowerOfTwo :: (IndexableBits a, FiniteBits a) => a -> Maybe Word
- getPowerOfFour :: (IndexableBits a, FiniteBits a) => a -> Maybe Word
Documentation
type Bits a = (Eq a, FiniteBits a, IndexableBits a, ShiftableBits a, Bitwise a, RotatableBits a, KnownNat (BitSize a), MaskBits a) Source #
class FiniteBits a where Source #
Type representable by a fixed amount of bits
bitSize :: (Integral i, KnownNat (BitSize a)) => a -> i Source #
Number of bits (the value is ignored)
All bits set to 0
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
class IndexableBits a where Source #
Type whose bits are indexable
Nothing
bit i
is a value with the i
th bit set and all other bits clear.
bit :: (Num a, ShiftableBits a) => Word -> a Source #
bit i
is a value with the i
th 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 n
th bit of the argument is 1
testBit :: (Bitwise a, Num a, Eq a) => a -> Word -> Bool Source #
Return True
if the n
th 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
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.
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
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.
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
class RotatableBits a where Source #
Types whose bits can be rotated
Nothing
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
class Bitwise a where Source #
Bitwise bit operations
Bitwise "and"
Bitwise "or"
Bitwise "xor"
Instances
Bitwise Int Source # | |
Bitwise Int8 Source # | |
Bitwise Int16 Source # | |
Bitwise Int32 Source # | |
Bitwise Int64 Source # | |
Bitwise Integer Source # | |
Bitwise Natural Source # | |
Bitwise Word Source # | |
Bitwise Word8 Source # | |
Bitwise Word16 Source # | |
Bitwise Word32 Source # | |
Bitwise Word64 Source # | |
Bitwise Buffer Source # | |
Bitwise a => Bitwise (AsLittleEndian a) Source # | |
Defined in Haskus.Binary.Endianness (.&.) :: AsLittleEndian a -> AsLittleEndian a -> AsLittleEndian a Source # (.|.) :: AsLittleEndian a -> AsLittleEndian a -> AsLittleEndian a Source # xor :: AsLittleEndian a -> AsLittleEndian a -> AsLittleEndian a Source # | |
Bitwise a => Bitwise (AsBigEndian a) Source # | |
Defined in Haskus.Binary.Endianness (.&.) :: AsBigEndian a -> AsBigEndian a -> AsBigEndian a Source # (.|.) :: AsBigEndian a -> AsBigEndian a -> AsBigEndian a Source # xor :: AsBigEndian a -> AsBigEndian a -> AsBigEndian a Source # | |
(KnownNat n, Bitwise a, Storable a) => Bitwise (Vector n a) Source # | |
Bit reversal
class ReversableBits w where Source #
Data whose bits can be reversed
reverseBits :: w -> w Source #
Instances
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 #
makeMaskDyn :: Word -> a Source #
Make a mask dynamically
Instances
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
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