[project @ 2001-01-11 17:25:56 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelBits.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998-2000
3 %
4 \section[Bits]{The @Bits@ interface}
5
6 Defines the @Bits@ class containing bit-based operations.
7 See library document for details on the semantics of the
8 individual operations.
9
10 \begin{code}
11 module PrelBits where
12
13 #ifdef __GLASGOW_HASKELL__
14 import PrelGHC
15 import PrelBase
16 import PrelNum
17 #endif
18
19 --ADR: The fixity for .|. conflicts with that for .|. in Fran.
20 --     Removing all fixities is a fairly safe fix; fixing the "one fixity
21 --     per symbol per program" limitation in Hugs would take a lot longer.
22 #ifndef __HUGS__
23 infixl 8 `shift`, `rotate`
24 infixl 7 .&.
25 infixl 6 `xor`
26 infixl 5 .|.
27 #endif
28
29 class Num a => Bits a where
30   (.&.), (.|.), xor :: a -> a -> a
31   complement        :: a -> a
32   shift             :: a -> Int -> a
33   rotate            :: a -> Int -> a
34   bit               :: Int -> a
35   setBit            :: a -> Int -> a
36   clearBit          :: a -> Int -> a
37   complementBit     :: a -> Int -> a
38   testBit           :: a -> Int -> Bool
39   bitSize           :: a -> Int
40   isSigned          :: a -> Bool
41
42   bit i             = shift 0x1 i
43   setBit x i        = x .|. bit i
44   clearBit x i      = x .&. complement (bit i)
45   complementBit x i = x `xor` bit i
46   testBit x i       = (x .&. bit i) /= 0
47
48 shiftL, shiftR   :: Bits a => a -> Int -> a
49 rotateL, rotateR :: Bits a => a -> Int -> a
50 shiftL  a i = shift  a i
51 shiftR  a i = shift  a (-i)
52 rotateL a i = rotate a i
53 rotateR a i = rotate a (-i)
54 \end{code}