base-4.6.0.0: Basic libraries

Portabilityportable
Stabilityexperimental
Maintainer[email protected]
Safe HaskellTrustworthy

Data.Bits

Description

This module defines bitwise operations for signed and unsigned integers. Instances of the class Bits for the Int and Integer types are available from this module, and instances for explicitly sized integral types are available from the Data.Int and Data.Word modules.

Synopsis

Documentation

class Eq a => Bits a whereSource

The Bits class defines bitwise operations over integral types.

  • Bits are numbered from 0 with bit 0 being the least significant bit.

Minimal complete definition: .&., .|., xor, complement, (shift or (shiftL and shiftR)), (rotate or (rotateL and rotateR)), bitSize, isSigned, testBit, bit, and popCount. The latter three can be implemented using testBitDefault, 'bitDefault, and popCountDefault, if a is also an instance of Num.

Methods

(.&.) :: a -> a -> aSource

Bitwise "and"

(.|.) :: a -> a -> aSource

Bitwise "or"

xor :: a -> a -> aSource

Bitwise "xor"

complement :: a -> aSource

Reverse all the bits in the argument

shift :: a -> Int -> aSource

shift x i shifts x 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 -> aSource

rotate x i rotates x 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.

bit :: Int -> aSource

bit i is a value with the ith bit set and all other bits clear

setBit :: a -> Int -> aSource

x `setBit` i is the same as x .|. bit i

clearBit :: a -> Int -> aSource

x `clearBit` i is the same as x .&. complement (bit i)

complementBit :: a -> Int -> aSource

x `complementBit` i is the same as x `xor` bit i

testBit :: a -> Int -> BoolSource

Return True if the nth bit of the argument is 1

bitSize :: a -> IntSource

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.

isSigned :: a -> BoolSource

Return True if the argument is a signed type. The actual value of the argument is ignored

shiftL :: a -> Int -> aSource

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 -> aSource

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.

shiftR :: a -> Int -> aSource

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 -> aSource

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.

rotateL :: a -> Int -> aSource

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 -> aSource

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.

popCount :: a -> IntSource

Return the number of set bits in the argument. This number is known as the population count or the Hamming weight.

bitDefault :: (Bits a, Num a) => Int -> aSource

Default implementation for bit.

Note that: bitDefault i = 1 shiftL i

testBitDefault :: (Bits a, Num a) => a -> Int -> BoolSource

Default implementation for testBit.

Note that: testBitDefault x i = (x .&. bit i) /= 0

popCountDefault :: (Bits a, Num a) => a -> IntSource

Default implementation for popCount.

This implementation is intentionally naive. Instances are expected to provide an optimized implementation for their size.