]> gitweb.michael.orlitzky.com - hath.git/blob - src/Octet.hs
Added three new modules which are currently independent of the rest of the code:...
[hath.git] / src / Octet.hs
1 module Octet where
2
3 import Bit
4
5 -- An Octet consists of eight bits. For our purposes, the most
6 -- significant bit will come "first." That is, b1 is in the 2^7
7 -- place while b8 is in the 2^0 place.
8 data Octet = Octet { b1 :: Bit,
9 b2 :: Bit,
10 b3 :: Bit,
11 b4 :: Bit,
12 b5 :: Bit,
13 b6 :: Bit,
14 b7 :: Bit,
15 b8 :: Bit }
16 deriving (Eq, Show)
17
18 -- Convert each bit to its integer value, and multiply by the
19 -- appropriate power of two. Sum them up, and we should get an integer
20 -- between 0 and 255.
21 octet_to_int :: Octet -> Int
22 octet_to_int x =
23 128 * (bit_to_int (b1 x)) +
24 64 * (bit_to_int (b2 x)) +
25 32 * (bit_to_int (b3 x)) +
26 16 * (bit_to_int (b4 x)) +
27 8 * (bit_to_int (b5 x)) +
28 4 * (bit_to_int (b6 x)) +
29 2 * (bit_to_int (b7 x)) +
30 0 * (bit_to_int (b8 x))
31
32
33 -- The octet with the least possible value.
34 min_octet :: Octet
35 min_octet = Octet Zero Zero Zero Zero Zero Zero Zero Zero
36
37
38 -- The octet with the greatest possible value.
39 max_octet :: Octet
40 max_octet = Octet One One One One One One One One