Changeset 64 for Deliverables/D4.1/Nibble.ml
 Timestamp:
 Sep 13, 2010, 12:59:27 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Deliverables/D4.1/Nibble.ml
r63 r64 17 17 val from_bit: bit > nibble 18 18 val from_int: int > nibble option 19 val from_string: string > nibble option19 (* val from_string: string > nibble option *) 20 20 21 21 val to_bits: nibble > (bit * bit * bit * bit) … … 30 30 val (): nibble > nibble > nibble 31 31 val (^): nibble > nibble > nibble 32 val rotate_right: nibble > nibble 33 val rotate_left: nibble > nibble 34 val shift_right: nibble > nibble 35 val shift_left: nibble > nibble 32 36 val not: nibble > nibble 33 34 val swap_bits: nibble > nibble35 37 36 38 val map_bit: (bit > bit) > nibble > nibble … … 65 67  15 > Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1) 66 68  _ > None 67 let from_string68 69 69 let to_bit (l, h) = 70 let (bl, bh) = Bit.to_bool l, Bit.to_bool h in 71 match (bl, bh) with 72 (false, b) > Some (Bit.from_bool b) 73  (true, b) > None 74 let to_int (l, h) = (2 * Bit.to_int l) + Bit.to_int h 70 let to_bit (b1, b2, b3, b4) = 71 let (d1, d2, d3, d4) = Bit.to_bool b1, Bit.to_bool b2, Bit.to_bool b3, Bit.to_bool b4 in 72 match (d1, d2, d3, d4) with 73 (false, false, false, b) > Some (Bit.from_bool b) 74  _ > None 75 let to_bits (b1, b2, b3, b4) = (b1, b2, b3, b4) 76 let to_int (b1, b2, b3, b4) = (8 * Bit.to_int b1) + (4 * Bit.to_int b2) + 77 (2 * Bit.to_int b3) + Bit.to_int b4 78 79 let get_bit_at index (b1, b2, b3, b4) = 80 match index with 81 0 > Some b4 82  1 > Some b3 83  2 > Some b2 84  3 > Some b1 85  _ > None 86 87 let set_bit_at index new_bit (b1, b2, b3, b4) = 88 match index with 89 0 > Some (b1, b2, b3, new_bit) 90  1 > Some (b1, b2, new_bit, b4) 91  2 > Some (b1, new_bit, b3, b4) 92  3 > Some (new_bit, b2, b3, b4) 93  _ > None 94 95 let (&) (l1, l2, l3, l4) (r1, r2, r3, r4) = 96 (Bit.(&) l1 r1, Bit.(&) l2 r2, Bit.(&) l3 r3, Bit.(&) l4 r4) 97 let () (l1, l2, l3, l4) (r1, r2, r3, r4) = 98 (Bit.() l1 r1, Bit.() l2 r2, Bit.() l3 r3, Bit.() l4 r4) 99 let (^) (l1, l2, l3, l4) (r1, r2, r3, r4) = 100 (Bit.(^) l1 r1, Bit.(^) l2 r2, Bit.(^) l3 r3, Bit.(^) l4 r4) 101 let rotate_right (b1, b2, b3, b4) = (b4, b1, b2, b3) 102 let rotate_left (b1, b2, b3, b4) = (b2, b3, b4, b1) 103 let shift_right (b1, b2, b3, b4) = (Bit.from_bool false, b1, b2, b3) 104 let shift_left (b1, b2, b3, b4) = (b2, b3, b4, Bit.from_bool false) 105 let not (b1, b2, b3, b4) = (Bit.not b1, Bit.not b2, Bit.not b3, Bit.not b4) 106 107 let map_bit f (b1, b2, b3, b4) = (f b1, f b2, f b3, f b4) 108 let iter_bit f (b1, b2, b3, b4) = 109 let str_b1 = f b1 in 110 let str_b2 = f b2 in 111 let str_b3 = f b3 in 112 let str_b4 = f b4 in 113 str_b1 ^ str_b2 ^ str_b3 ^ str_b4 114 let zip_bit f (l1, l2, l3, l4) (r1, r2, r3, r4) = (f l1 r1, f l2 r2, f l3 r3, f l4 r4) 115 75 116 let to_string = iter_bit Bit.to_string 76 77 let get_bit_at index (l, h) =78 if index = 0 then79 Some l80 else if index = 1 then81 Some h82 else83 None84 85 let set_bit_at index new_bit (l, h) =86 if index = 0 then87 Some (new_bit, h)88 else if index = 1 then89 Some (l, new_bit)90 else91 None92 93 let (&) (l1,h1) (l2, h2) =94 (Bit.(&) l1 l2, Bit.(&) h1 h2)95 let () (l1,h1) (l2, h2) =96 (Bit.() l1 l2, Bit.() h1 h2)97 let (^) (l1,h1) (l2, h2) =98 (Bit.(^) l1 l2, Bit.(^) h1 h2)99 100 let not (l, h) = (Bit.not l, Bit.not h)101 102 let swap_bits (l, h) = (h, l)103 104 let map_bit f (l, h) = (f l, f h)105 let iter_bit f (l, h) =106 let str_l = f l in107 let str_h = f h in108 str_l ^ str_h109 let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2)110 117 end 111 118
Note: See TracChangeset
for help on using the changeset viewer.