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 -> 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
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)
</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>
|