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))
- class FiniteBits a where
- 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
- (.&.) :: a -> a -> a
- (.|.) :: a -> a -> a
- xor :: a -> a -> a
- complement :: a -> a
- 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
- makeMask :: forall a. (ShiftableBits a, FiniteBits a, KnownNat (BitSize a), Bitwise a) => Word -> a
- maskLeastBits :: forall a. (ShiftableBits a, FiniteBits a, Bitwise a, KnownNat (BitSize a)) => Word -> 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) => BitOrder -> Word -> Word -> b -> b
- bitOffset :: Word -> Word
- byteOffset :: Word -> Word
Documentation
type Bits a = (Eq a, FiniteBits a, IndexableBits a, ShiftableBits a, Bitwise a, RotatableBits a, KnownNat (BitSize 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
oneBits :: Bitwise a => 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
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 :: 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
Instances
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
makeMask :: forall a. (ShiftableBits a, FiniteBits a, KnownNat (BitSize a), Bitwise a) => Word -> a Source #
makeMask 3 = 00000111
maskLeastBits :: forall a. (ShiftableBits a, FiniteBits a, Bitwise a, KnownNat (BitSize a)) => Word -> 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) => 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