[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / docs / libraries / Bits.sgml
1 <sect> <idx/Bits/
2 <label id="sec:Bits">
3 <p>
4
5 This library defines bitwise operations for signed and unsigned ints.
6
7 <tscreen><verb>
8 module Bits where
9 infixl 8 `shift`, `rotate`
10 infixl 7 .&.
11 infixl 6 `xor`
12 infixl 5 .|.
13
14 class Bits a where
15   (.&.), (.|.), xor :: a -> a -> a
16   complement        :: a -> a
17   shift             :: a -> Int -> a
18   rotate            :: a -> Int -> a
19   bit               :: Int -> a        
20   setBit            :: a -> Int -> a   
21   clearBit          :: a -> Int -> a   
22   complementBit     :: a -> Int -> a   
23   testBit           :: a -> Int -> Bool
24   bitSize           :: a -> Int
25   isSigned          :: a -> Bool
26
27 shiftL, shiftR   :: Bits a => a -> Int -> a
28 rotateL, rotateR :: Bits a => a -> Int -> a
29 shiftL  a i = shift  a i
30 shiftR  a i = shift  a (-i)
31 rotateL a i = rotate a i
32 rotateR a i = rotate a (-i)
33 </verb></tscreen>
34
35 Notes:
36 <itemize>
37 <item>
38   <tt/bitSize/ and <tt/isSigned/ are like <tt/floatRadix/ and
39   <tt/floatDigits/ -- they return parameters of the <em/type/ of their
40   argument rather than of the particular argument they are applied to.
41   <tt/bitSize/ returns the number of bits in the type; and
42   <tt/isSigned/ returns whether the type is signed or not.
43 <item>
44   <tt/shift/ performs sign extension on signed number types.
45   That is, right shifts fill the top bits with 1 if the number is negative
46   and with 0 otherwise.
47 <item>
48   Bits are numbered from 0 with bit 0 being the least significant bit.
49 <item>
50   <tt/shift x i/ and <tt/rotate x i/ shift to the left if <tt/i/ is
51   positive and to the right otherwise.  
52 <!--
53   <item>
54     <tt/rotate/ is well defined only if bitSize returns a number.
55     (Maybe we should impose a Bounded constraint on it?)
56   -->
57 <item>
58   <tt/bit i/ is the value with the i'th bit set.
59 </itemize>