Copyright | (c) The University of Glasgow 2001 |
---|---|
License | BSD-style (see the file libraries/base/LICENSE) |
Maintainer | libraries@haskell.org |
Stability | experimental |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Synopsis
- class Eq a => Bits a where
- (.&.) :: a -> a -> a
- (.|.) :: a -> a -> a
- xor :: a -> a -> a
- complement :: a -> a
- shift :: a -> Int -> a
- rotate :: a -> Int -> a
- zeroBits :: a
- bit :: Int -> a
- setBit :: a -> Int -> a
- clearBit :: a -> Int -> a
- complementBit :: a -> Int -> a
- testBit :: a -> Int -> Bool
- bitSizeMaybe :: a -> Maybe Int
- bitSize :: a -> Int
- isSigned :: a -> Bool
- shiftL :: a -> Int -> a
- unsafeShiftL :: a -> Int -> a
- shiftR :: a -> Int -> a
- unsafeShiftR :: a -> Int -> a
- rotateL :: a -> Int -> a
- rotateR :: a -> Int -> a
- popCount :: a -> Int
- class Bits b => FiniteBits b where
- finiteBitSize :: b -> Int
- countLeadingZeros :: b -> Int
- countTrailingZeros :: b -> Int
- bitDefault :: (Bits a, Num a) => Int -> a
- testBitDefault :: (Bits a, Num a) => a -> Int -> Bool
- popCountDefault :: (Bits a, Num a) => a -> Int
- toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
- oneBits :: FiniteBits a => a
- newtype And a = And {
- getAnd :: a
- newtype Ior a = Ior {
- getIor :: a
- newtype Xor a = Xor {
- getXor :: a
- newtype Iff a = Iff {
- getIff :: a
Type classes
class Eq a => Bits a where Source #
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 Source #
Bitwise "and"
(.|.) :: a -> a -> a infixl 5 Source #
Bitwise "or"
xor :: a -> a -> a infixl 6 Source #
Bitwise "xor"
complement :: a -> a Source #
Reverse all the bits in the argument
shift :: a -> Int -> a infixl 8 Source #
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 Source #
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: base-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
.
setBit :: a -> Int -> a Source #
x `setBit` i
is the same as x .|. bit i
clearBit :: a -> Int -> a Source #
x `clearBit` i
is the same as x .&. complement (bit i)
complementBit :: a -> Int -> a Source #
x `complementBit` i
is the same as x `xor` bit i
testBit :: a -> Int -> Bool Source #
x `testBit` i
is the same as x .&. bit n /= 0
In other words it returns True if the bit at offset @n is set.
Can be implemented using testBitDefault
if a
is also an
instance of Num
.
bitSizeMaybe :: a -> Maybe Int Source #
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: base-4.7.0.0
Deprecated: Use bitSizeMaybe
or finiteBitSize
instead
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
.
Default implementation based upon bitSizeMaybe
provided since
4.12.0.0.
isSigned :: a -> Bool Source #
Return True
if the argument is a signed type. The actual
value of the argument is ignored
shiftL :: a -> Int -> a infixl 8 Source #
Shift the argument left by the specified number of bits
(which must be non-negative). Some instances may throw an
Overflow
exception if given a negative input.
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 Source #
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: base-4.5.0.0
shiftR :: a -> Int -> a infixl 8 Source #
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
. 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 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 Source #
Shift 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 x
is negative
and with 0 otherwise.
Defaults to shiftR
unless defined explicitly by an instance.
Since: base-4.5.0.0
rotateL :: a -> Int -> a infixl 8 Source #
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 Source #
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: base-4.5.0.0
Instances
class Bits b => FiniteBits b where Source #
The FiniteBits
class denotes types with a finite, fixed number of bits.
Since: base-4.7.0.0
finiteBitSize :: b -> Int Source #
Return the number of bits in the type of the argument.
The actual value of the argument is ignored. Moreover, finiteBitSize
is total, in contrast to the deprecated bitSize
function it replaces.
finiteBitSize
=bitSize
bitSizeMaybe
=Just
.finiteBitSize
Since: base-4.7.0.0
countLeadingZeros :: b -> Int Source #
Count number of zero bits preceding the most significant set bit.
countLeadingZeros
(zeroBits
:: a) = finiteBitSize (zeroBits
:: a)
countLeadingZeros
can be used to compute log base 2 via
logBase2 x =finiteBitSize
x - 1 -countLeadingZeros
x
Note: The default implementation for this method is intentionally naive. However, the instances provided for the primitive integral types are implemented using CPU specific machine instructions.
Since: base-4.8.0.0
countTrailingZeros :: b -> Int Source #
Count number of zero bits following the least significant set bit.
countTrailingZeros
(zeroBits
:: a) = finiteBitSize (zeroBits
:: a)countTrailingZeros
.negate
=countTrailingZeros
The related
find-first-set operation
can be expressed in terms of countTrailingZeros
as follows
findFirstSet x = 1 + countTrailingZeros
x
Note: The default implementation for this method is intentionally naive. However, the instances provided for the primitive integral types are implemented using CPU specific machine instructions.
Since: base-4.8.0.0
Instances
Extra functions
testBitDefault :: (Bits a, Num a) => a -> Int -> Bool Source #
Default implementation for testBit
.
Note that: testBitDefault x i = (x .&. bit i) /= 0
Since: base-4.6.0.0
popCountDefault :: (Bits a, Num a) => a -> Int Source #
Default implementation for popCount
.
This implementation is intentionally naive. Instances are expected to provide an optimized implementation for their size.
Since: base-4.6.0.0
toIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b Source #
Attempt to convert an Integral
type a
to an Integral
type b
using
the size of the types as measured by Bits
methods.
A simpler version of this function is:
toIntegral :: (Integral a, Integral b) => a -> Maybe b toIntegral x | toInteger x == y = Just (fromInteger y) | otherwise = Nothing where y = toInteger x
This version requires going through Integer
, which can be inefficient.
However, toIntegralSized
is optimized to allow GHC to statically determine
the relative type sizes (as measured by bitSizeMaybe
and isSigned
) and
avoid going through Integer
for many types. (The implementation uses
fromIntegral
, which is itself optimized with rules for base
types but may
go through Integer
for some type pairs.)
Since: base-4.8.0.0
oneBits :: FiniteBits a => a Source #
A more concise version of complement zeroBits
.
>>>
complement (zeroBits :: Word) == (oneBits :: Word)
True
>>>
complement (oneBits :: Word) == (zeroBits :: Word)
True
Note
The constraint on oneBits
is arguably too strong. However, as some types
(such as Natural
) have undefined complement
, this is the only safe
choice.
Since: base-4.16
Newtypes
Monoid under bitwise AND.
>>>
getAnd (And 0xab <> And 0x12) :: Word8
2
Since: base-4.16
Instances
FiniteBits a => Monoid (And a) # | This constraint is arguably too strong. However,
as some types (such as Since: base-4.16 |
Bits a => Semigroup (And a) # | Since: base-4.16 |
Bits a => Bits (And a) # | Since: base-4.16 |
Defined in Data.Bits (.&.) :: And a -> And a -> And a Source # (.|.) :: And a -> And a -> And a Source # xor :: And a -> And a -> And a Source # complement :: And a -> And a Source # shift :: And a -> Int -> And a Source # rotate :: And a -> Int -> And a Source # setBit :: And a -> Int -> And a Source # clearBit :: And a -> Int -> And a Source # complementBit :: And a -> Int -> And a Source # testBit :: And a -> Int -> Bool Source # bitSizeMaybe :: And a -> Maybe Int Source # bitSize :: And a -> Int Source # isSigned :: And a -> Bool Source # shiftL :: And a -> Int -> And a Source # unsafeShiftL :: And a -> Int -> And a Source # shiftR :: And a -> Int -> And a Source # unsafeShiftR :: And a -> Int -> And a Source # rotateL :: And a -> Int -> And a Source # | |
FiniteBits a => FiniteBits (And a) # | Since: base-4.16 |
Bounded a => Bounded (And a) # | Since: base-4.16 |
Enum a => Enum (And a) # | Since: base-4.16 |
Defined in Data.Bits succ :: And a -> And a Source # pred :: And a -> And a Source # toEnum :: Int -> And a Source # fromEnum :: And a -> Int Source # enumFrom :: And a -> [And a] Source # enumFromThen :: And a -> And a -> [And a] Source # enumFromTo :: And a -> And a -> [And a] Source # enumFromThenTo :: And a -> And a -> And a -> [And a] Source # | |
Read a => Read (And a) # | Since: base-4.16 |
Show a => Show (And a) # | Since: base-4.16 |
Eq a => Eq (And a) # | Since: base-4.16 |
Monoid under bitwise inclusive OR.
>>>
getIor (Ior 0xab <> Ior 0x12) :: Word8
187
Since: base-4.16
Instances
Monoid under bitwise XOR.
>>>
getXor (Xor 0xab <> Xor 0x12) :: Word8
185
Since: base-4.16
Instances
Monoid under bitwise 'equality'; defined as 1
if the corresponding
bits match, and 0
otherwise.
>>>
getIff (Iff 0xab <> Iff 0x12) :: Word8
70
Since: base-4.16
Instances
FiniteBits a => Monoid (Iff a) # | This constraint is arguably
too strong. However, as some types (such as Since: base-4.16 |
FiniteBits a => Semigroup (Iff a) # | This constraint is arguably
too strong. However, as some types (such as Since: base-4.16 |
Bits a => Bits (Iff a) # | Since: base-4.16 |
Defined in Data.Bits (.&.) :: Iff a -> Iff a -> Iff a Source # (.|.) :: Iff a -> Iff a -> Iff a Source # xor :: Iff a -> Iff a -> Iff a Source # complement :: Iff a -> Iff a Source # shift :: Iff a -> Int -> Iff a Source # rotate :: Iff a -> Int -> Iff a Source # setBit :: Iff a -> Int -> Iff a Source # clearBit :: Iff a -> Int -> Iff a Source # complementBit :: Iff a -> Int -> Iff a Source # testBit :: Iff a -> Int -> Bool Source # bitSizeMaybe :: Iff a -> Maybe Int Source # bitSize :: Iff a -> Int Source # isSigned :: Iff a -> Bool Source # shiftL :: Iff a -> Int -> Iff a Source # unsafeShiftL :: Iff a -> Int -> Iff a Source # shiftR :: Iff a -> Int -> Iff a Source # unsafeShiftR :: Iff a -> Int -> Iff a Source # rotateL :: Iff a -> Int -> Iff a Source # | |
FiniteBits a => FiniteBits (Iff a) # | Since: base-4.16 |
Bounded a => Bounded (Iff a) # | Since: base-4.16 |
Enum a => Enum (Iff a) # | Since: base-4.16 |
Defined in Data.Bits succ :: Iff a -> Iff a Source # pred :: Iff a -> Iff a Source # toEnum :: Int -> Iff a Source # fromEnum :: Iff a -> Int Source # enumFrom :: Iff a -> [Iff a] Source # enumFromThen :: Iff a -> Iff a -> [Iff a] Source # enumFromTo :: Iff a -> Iff a -> [Iff a] Source # enumFromThenTo :: Iff a -> Iff a -> Iff a -> [Iff a] Source # | |
Read a => Read (Iff a) # | Since: base-4.16 |
Show a => Show (Iff a) # | Since: base-4.16 |
Eq a => Eq (Iff a) # | Since: base-4.16 |