[project @ 2001-01-20 20:55:58 by panne]
[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 import Prelude          -- To generate the dependency
14 #ifdef __GLASGOW_HASKELL__
15 import PrelGHC
16 import PrelBase
17 import PrelNum
18 #endif
19
20 --ADR: The fixity for .|. conflicts with that for .|. in Fran.
21 --     Removing all fixities is a fairly safe fix; fixing the "one fixity
22 --     per symbol per program" limitation in Hugs would take a lot longer.
23 #ifndef __HUGS__
24 infixl 8 `shift`, `rotate`
25 infixl 7 .&.
26 infixl 6 `xor`
27 infixl 5 .|.
28 #endif
29
30 class Num a => Bits a where
31   (.&.), (.|.), xor :: a -> a -> a
32   complement        :: a -> a
33   shift             :: a -> Int -> a
34   rotate            :: a -> Int -> a
35   bit               :: Int -> a
36   setBit            :: a -> Int -> a
37   clearBit          :: a -> Int -> a
38   complementBit     :: a -> Int -> a
39   testBit           :: a -> Int -> Bool
40   bitSize           :: a -> Int
41   isSigned          :: a -> Bool
42
43   bit i             = shift 0x1 i
44   setBit x i        = x .|. bit i
45   clearBit x i      = x .&. complement (bit i)
46   complementBit x i = x `xor` bit i
47   testBit x i       = (x .&. bit i) /= 0
48
49 shiftL, shiftR   :: Bits a => a -> Int -> a
50 rotateL, rotateR :: Bits a => a -> Int -> a
51 shiftL  a i = shift  a i
52 shiftR  a i = shift  a (-i)
53 rotateL a i = rotate a i
54 rotateR a i = rotate a (-i)
55 \end{code}