File: libs-4.html

package info (click to toggle)
hugs 1.4.199801-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 7,220 kB
  • ctags: 5,609
  • sloc: ansic: 32,083; haskell: 12,143; yacc: 949; perl: 823; sh: 602; makefile: 236
file content (71 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<HTML>
<HEAD>
<TITLE>The Hugs-GHC Extension Libraries: Bits </TITLE>
</HEAD>
<BODY>
<A HREF="libs-3.html">Previous</A>
<A HREF="libs-5.html">Next</A>
<A HREF="libs.html#toc4">Table of Contents</A>
<HR>
<H2><A NAME="s4">4. Bits </A></H2>


<P>This library defines bitwise operations for signed and unsigned ints.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
module Bits where
infixl 8 `shift`, `rotate`
infixl 7 .&.
infixl 6 `xor`
infixl 5 .|.

class Bits a where
  (.&.), (.|.), xor :: a -&gt; a -&gt; a
  complement        :: a -&gt; a
  shift             :: a -&gt; Int -&gt; a
  rotate            :: a -&gt; Int -&gt; a
  bit               :: Int -&gt; a        
  setBit            :: a -&gt; Int -&gt; a   
  clearBit          :: a -&gt; Int -&gt; a   
  complementBit     :: a -&gt; Int -&gt; a   
  testBit           :: a -&gt; Int -&gt; Bool
  bitSize           :: a -&gt; Int
  isSigned          :: a -&gt; Bool

shiftL, shiftR   :: Bits a =&gt; a -&gt; Int -&gt; a
rotateL, rotateR :: Bits a =&gt; a -&gt; Int -&gt; 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)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Notes:
<UL>
<LI><CODE>bitSize</CODE> and <CODE>isSigned</CODE> are like <CODE>floatRadix</CODE> and <CODE>floatDigits</CODE>
-- they return parameters of the <EM>type</EM> of their argument rather than 
of the particular argument they are applied to.  <CODE>bitSize</CODE> returns
the number of bits in the type (or <CODE>Nothing</CODE> for unbounded types); and
<CODE>isSigned</CODE> returns whether the type is signed or not.  </LI>
<LI><CODE>shift</CODE> performs sign extension.  
That is, right shifts fill the top bits with 1 if the  number is negative
and with 0 otherwise.
(Since unsigned types are always positive, the top bit is always filled with
0.)</LI>
<LI> 
Bits are numbered from 0 with bit 0 being the least significant bit.</LI>
<LI><CODE>shift x i</CODE> and <CODE>rotate x i</CODE> shift to the left if <CODE>i</CODE> is
positive and to the right otherwise.  
</LI>
<LI><CODE>bit i</CODE> is the value with the i'th bit set.</LI>
</UL>
</P>

<HR>
<A HREF="libs-3.html">Previous</A>
<A HREF="libs-5.html">Next</A>
<A HREF="libs.html#toc4">Table of Contents</A>
</BODY>
</HTML>