Bin <-> Decimal conversion
- To: MATHGROUP at CHRISTENSEN.CYBERNETICS.NET
- Subject: [mg395] Bin <-> Decimal conversion
- From: barenco at vax.ox.ac.uk
- Date: Tue, 10 Jan 1995 18:08:45 +0000
Dear Mathgroup:
I'm looking for an efficient way to convert number in base 10
to their binary form and vice versa. I.e. two functions such
that (for instance),
ToBin[54] -----> {1,1,0,1,1,0}
ToDec[{1,1,0,1,1,0}] -----> 54
I need to have the binary digits as a List (therefore the
instruction BaseForm doesn't do the job).
Along the lines of the example in the Mma book (p. 202) I
found an elegant solution for ToDec :
ToDec[list_]:=Fold[(2 #1 +#2)&,0,list]
I am still looking for an efficient way to perform ToBin.
Thank you for any suggestion.
Adriano Barenco
barenco at vax.ox.ac.uk