Unsafe Shift Left Operator
Modulefoundation-0.0.30Haskell2010
Foundation.Bits
- 1 class
- 4 values
- Packagefoundation-0.0.30
- Exports5
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceBits.hs
Unsafe Shift Right Operator
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 -> ainfixl 7Bitwise "and"
(.|.) :: a -> a -> ainfixl 5Bitwise "or"
xor :: a -> a -> ainfixl 6Bitwise "xor"
complement :: a -> aReverse all the bits in the argument
shift :: a -> Int -> ainfixl 8shift x ishiftsxleft byibits ifiis positive, or right by-ibits otherwise. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if thexis 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 -> ainfixl 8zeroBits :: azeroBits is the value with all bits unset.
The following laws ought to hold (for all valid bit indices
n):clearBit zeroBits n == zeroBitssetBit zeroBits n == bit ntestBit zeroBits n == FalsepopCount zeroBits == 0
This method uses
clearBit (bit 0) 0as its default implementation (which ought to be equivalent to zeroBits for types which possess a 0th bit).bit :: Int -> asetBit :: a -> Int -> ax `setBit` iis the same asx .|. bit iclearBit :: a -> Int -> ax `clearBit` iis the same asx .&. complement (bit i)complementBit :: a -> Int -> ax `complementBit` iis the same asx `xor` bit itestBit :: a -> Int -> Boolx `testBit` iis the same asx .&. bit n /= 0In other words it returns True if the bit at offset @n is set.
Can be implemented using
testBitDefaultifais also an instance of Num.bitSizeMaybe :: a -> Maybe IntReturn 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.
bitSize :: a -> IntReturn 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 -> BoolReturn True if the argument is a signed type. The actual value of the argument is ignored
shiftL :: a -> Int -> ainfixl 8unsafeShiftL :: a -> Int -> ashiftR :: a -> Int -> ainfixl 8Shift 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
xis 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 -> aShift 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
xis negative and with 0 otherwise.Defaults to shiftR unless defined explicitly by an instance.
rotateL :: a -> Int -> ainfixl 8rotateR :: a -> Int -> ainfixl 8popCount :: a -> IntReturn the number of set bits in the argument. This number is known as the population count or the Hamming weight.
Can be implemented using
popCountDefaultifais also an instance of Num.
Instances69Bits, …
Bits Word128Defined in basement-0.0.16 · Basement.Types.Word128Bits Word256Defined in basement-0.0.16 · Basement.Types.Word256Bits IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.BitsBits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.BitsBits EventTypeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPollBits EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.PollBits CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.TypesBits IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.PtrBits WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.PtrBits Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.IntBits Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.IntBits Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.IntBits Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.IntBits CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.TypesBits Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.WordBits Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.WordBits Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.WordBits Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.WordBits BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.BitsInterpret Bool as 1-bit bit-field
Bits IntDefined in ghc-internal-9.1003.0 · GHC.Internal.BitsBits WordDefined in ghc-internal-9.1003.0 · GHC.Internal.BitsBits a => Bits (BE a)Defined in basement-0.0.16 · Basement.EndiannessBits a => Bits (LE a)Defined in basement-0.0.16 · Basement.EndiannessBits a => Bits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsBits a => Bits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsBits a => Bits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsBits a => Bits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.BitsBits a => Bits (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.IdentityBits a => Bits (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.OrdBits a => Bits (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
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
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