views:

366

answers:

1

How could I convert binary to decimal and decimal to binary in the following, If I am representing binary numbers like the following 01 is [False; True] which equals 2? I want to write a method to take in [False; True] and return 2. Also a method for doing it the other way around taking in an integer 2 and returning [False; True].

+1  A: 

Simpler bits to int:

let int_of_bool = function
   true -> 1
 | false -> 0

let int_of_bits b = 
  List.fold_right (fun x z -> 2 * z + int_of_bool x) b 0
newacct