12345678910111213141516171819202122232425262728293031323334 |
- structure Bit = struct
- fun to_decimal (bit, ind) =
- (* ind: the index of the bit counted from least significant bit to most
- significant bit (MSB) *)
- bit * IntExtra.expt (2, ind);
- fun bits_to_decimal bit_vector =
- let
- val len = Vector.length bit_vector;
- fun iter (bits, ind, acc) =
- if ind < len
- then
- let
- val bit = Vector.sub(bits, ind);
- val position = (len - 1) - ind;
- val decimal_value = to_decimal (bit, position);
- in
- iter (bits, ind + 1, acc + decimal_value)
- end
- else
- acc;
- in
- iter (bit_vector, 0, 0)
- end;
- fun bit_list_from_string str =
- map (fn c =>
- case c of
- #"1" => 1
- | #"0" => 0
- | _ => raise Fail "unrecognized bit value")
- (String.explode str);
- end;
|