module HaskellWorks.Data.Bits.BitWise
(
BitWise(..)
, Shift(..)
, TestBit(..)
) where
import Data.Word
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Bits.BitLength
import HaskellWorks.Data.Naive
import HaskellWorks.Data.Positioning
import Prelude as P
import qualified Data.Bits as B
import qualified Data.Vector as DV
import qualified Data.Vector.Storable as DVS
infixl 9 .?.
infixl 8 .<., .>.
infixl 7 .&.
infixl 6 .^.
infixl 5 .|.
class Shift a where
(.<.) :: a -> Count -> a
(.>.) :: a -> Count -> a
class TestBit a where
(.?.) :: a -> Position -> Bool
class BitWise a where
(.&.) :: a -> a -> a
(.|.) :: a -> a -> a
(.^.) :: a -> a -> a
comp :: a -> a
all0s :: a
all1s :: a
instance TestBit Bool where
(.?.) w 0 = w
(.?.) _ _ = error "Invalid bit index"
instance TestBit [Bool] where
(.?.) v p = v !! fromIntegral p
instance TestBit Int where
(.?.) w n = B.testBit w (fromIntegral n)
instance TestBit Word8 where
(.?.) w n = B.testBit w (fromIntegral n)
instance TestBit Word16 where
(.?.) w n = B.testBit w (fromIntegral n)
instance TestBit Word32 where
(.?.) w n = B.testBit w (fromIntegral n)
instance TestBit Word64 where
(.?.) w n = B.testBit w (fromIntegral n)
instance TestBit (Naive Word8) where
(.?.) w n = B.testBit (naive w) (fromIntegral n)
instance TestBit (Naive Word16) where
(.?.) w n = B.testBit (naive w) (fromIntegral n)
instance TestBit (Naive Word32) where
(.?.) w n = B.testBit (naive w) (fromIntegral n)
instance TestBit (Naive Word64) where
(.?.) w n = B.testBit (naive w) (fromIntegral n)
instance TestBit (DV.Vector Word8) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DV.Vector Word16) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DV.Vector Word32) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DV.Vector Word64) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DVS.Vector Word8) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DVS.Vector Word16) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DVS.Vector Word32) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance TestBit (DVS.Vector Word64) where
(.?.) v n = let (q, r) = n `quotRem` elemBitEnd v in (v !!! q) .?. r
instance BitWise Int where
(.&.) = (B..&.)
(.|.) = (B..|.)
(.^.) = B.xor
comp = B.complement
all0s = 0
all1s = 1
instance BitWise Word8 where
(.&.) = (B..&.)
(.|.) = (B..|.)
(.^.) = B.xor
comp = B.complement
all0s = 0
all1s = 0xff
instance BitWise Word16 where
(.&.) = (B..&.)
(.|.) = (B..|.)
(.^.) = B.xor
comp = B.complement
all0s = 0
all1s = 0xffff
instance BitWise Word32 where
(.&.) = (B..&.)
(.|.) = (B..|.)
(.^.) = B.xor
comp = B.complement
all0s = 0
all1s = 0xffffffff
instance BitWise Word64 where
(.&.) = (B..&.)
(.|.) = (B..|.)
(.^.) = B.xor
comp = B.complement
all0s = 0
all1s = 0xffffffffffffffff
instance Shift Int where
(.<.) w n = B.shiftL w (fromIntegral n)
(.>.) w n = B.shiftR w (fromIntegral n)
instance Shift Word8 where
(.<.) w n = B.shiftL w (fromIntegral n)
(.>.) w n = B.shiftR w (fromIntegral n)
instance Shift Word16 where
(.<.) w n = B.shiftL w (fromIntegral n)
(.>.) w n = B.shiftR w (fromIntegral n)
instance Shift Word32 where
(.<.) w n = B.shiftL w (fromIntegral n)
(.>.) w n = B.shiftR w (fromIntegral n)
instance Shift Word64 where
(.<.) w n = B.shiftL w (fromIntegral n)
(.>.) w n = B.shiftR w (fromIntegral n)