This module defines bitwise operations for signed and unsigned ints. Instances of class Bits for the Int and Integer types are available from this module, and instances for explicitly sized integral types are available from the Int (Section 4.18) and Word (Section 4.38) modules.
infixl 8 `shift`, `rotate` infixl 7 .&. infixl 6 `xor` infixl 5 .|. class Num a => Bits a where (.&.), (.|.), xor :: a -> a -> a complement :: a -> a shift :: a -> Int -> a rotate :: a -> Int -> a bit :: Int -> a setBit :: a -> Int -> a clearBit :: a -> Int -> a complementBit :: a -> Int -> a testBit :: a -> Int -> Bool bitSize :: a -> Int isSigned :: a -> Bool bit i = shift 0x1 i setBit x i = x .|. bit i clearBit x i = x .&. complement (bit i) complementBit x i = x `xor` bit i testBit x i = (x .&. bit i) /= 0 shiftL, shiftR :: Bits a => a -> Int -> a rotateL, rotateR :: Bits a => a -> Int -> a shiftL a i = shift a i shiftR a i = shift a (-i) rotateL a i = rotate a i rotateR a i = rotate a (-i) instance Bits Int instance Bits Integer
bitSize and isSigned are like floatRadix and floatDigits—they return parameters of the type of their argument rather than of the particular argument they are applied to. bitSize returns the number of bits in the type; and isSigned returns whether the type is signed or not.
shift performs sign extension on signed number types. That is, right shifts fill the top bits with 1 if the number is negative and with 0 otherwise.
Bits are numbered from 0 with bit 0 being the least significant bit.
shift x i and rotate x i shift to the left if i is positive and to the right otherwise.
rotate is well defined only if bitSize is also well defined (bitSize is undefined for Integer, for example).
bit i is the value with the i'th bit set.