Safe Haskell | None |
---|---|
Language | Haskell2010 |
Documentation
The Bits
class defines bitwise operations over integral types.
- Bits are numbered from 0 with bit 0 being the least significant bit.
(.&.), (.|.), xor, complement, (shift | shiftL, shiftR), (rotate | rotateL, rotateR), bitSize, bitSizeMaybe, isSigned, testBit, bit, popCount
(.&.) :: a -> a -> a infixl 7 #
Bitwise "and"
(.|.) :: a -> a -> a infixl 5 #
Bitwise "or"
Bitwise "xor"
complement :: a -> a #
Reverse all the bits in the argument
shift :: a -> Int -> a infixl 8 #
shifts shift
x ix
left by i
bits if i
is positive,
or right by -i
bits otherwise.
Right shifts perform sign extension on signed number types;
i.e. they fill the top bits with 1 if the x
is 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 -> a infixl 8 #
rotates rotate
x ix
left by i
bits if i
is positive,
or right by -i
bits otherwise.
For unbounded types like Integer
, rotate
is equivalent to shift
.
An instance can define either this unified rotate
or rotateL
and
rotateR
, depending on which is more convenient for the type in
question.
zeroBits
is the value with all bits unset.
The following laws ought to hold (for all valid bit indices n
):
clearBit
zeroBits
n ==zeroBits
setBit
zeroBits
n ==bit
ntestBit
zeroBits
n == FalsepopCount
zeroBits
== 0
This method uses
as its default
implementation (which ought to be equivalent to clearBit
(bit
0) 0zeroBits
for
types which possess a 0th bit).
Since: 4.7.0.0
bit i
is a value with the i
th bit set and all other bits clear.
Can be implemented using bitDefault
if a
is also an
instance of Num
.
See also zeroBits
.
x `setBit` i
is the same as x .|. bit i
x `clearBit` i
is the same as x .&. complement (bit i)
complementBit :: a -> Int -> a #
x `complementBit` i
is the same as x `xor` bit i
Return True
if the n
th bit of the argument is 1
Can be implemented using testBitDefault
if a
is also an
instance of Num
.
bitSizeMaybe :: a -> Maybe Int #
Return 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
.
Since: 4.7.0.0
Return 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
.
Return True
if the argument is a signed type. The actual
value of the argument is ignored
shiftL :: a -> Int -> a infixl 8 #
Shift the argument left by the specified number of bits (which must be non-negative).
An instance can define either this and shiftR
or the unified
shift
, depending on which is more convenient for the type in
question.
unsafeShiftL :: a -> Int -> a #
Shift the argument left by the specified number of bits. The
result is undefined for negative shift amounts and shift amounts
greater or equal to the bitSize
.
Defaults to shiftL
unless defined explicitly by an instance.
Since: 4.5.0.0
shiftR :: a -> Int -> a infixl 8 #
Shift 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
.
Right shifts perform sign extension on signed number types;
i.e. they fill the top bits with 1 if the x
is 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 -> a #
Shift the first argument right by the specified number of bits, which must be non-negative an 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 x
is negative
and with 0 otherwise.
Defaults to shiftR
unless defined explicitly by an instance.
Since: 4.5.0.0
rotateL :: a -> Int -> a infixl 8 #
Rotate the argument left by the specified number of bits (which must be non-negative).
An instance can define either this and rotateR
or the unified
rotate
, depending on which is more convenient for the type in
question.
rotateR :: a -> Int -> a infixl 8 #
Rotate the argument right by the specified number of bits (which must be non-negative).
An instance can define either this and rotateL
or the unified
rotate
, depending on which is more convenient for the type in
question.
Return the number of set bits in the argument. This number is known as the population count or the Hamming weight.
Can be implemented using popCountDefault
if a
is also an
instance of Num
.
Since: 4.5.0.0
Bits Bool | |
Bits Int | |
Bits Int8 | |
Bits Int16 | |
Bits Int32 | |
Bits Int64 | |
Bits Integer | |
Bits Word | |
Bits Word8 | |
Bits Word16 | |
Bits Word32 | |
Bits Word64 | |
Bits Natural | |
Bits CDev | |
Bits CIno | |
Bits CMode | |
Bits COff | |
Bits CPid | |
Bits CSsize | |
Bits CGid | |
Bits CNlink | |
Bits CUid | |
Bits CTcflag | |
Bits CRLim | |
Bits Fd | |
Bits WordPtr | |
Bits IntPtr | |
Bits CChar | |
Bits CSChar | |
Bits CUChar | |
Bits CShort | |
Bits CUShort | |
Bits CInt | |
Bits CUInt | |
Bits CLong | |
Bits CULong | |
Bits CLLong | |
Bits CULLong | |
Bits CPtrdiff | |
Bits CSize | |
Bits CWchar | |
Bits CSigAtomic | |
Bits CIntPtr | |
Bits CUIntPtr | |
Bits CIntMax | |
Bits CUIntMax | |
Bits a => Bits (Identity a) | |
Bits a => Bits (Const k a b) | |
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