divide each digit by 2 starting from the MSD using int div, if you have remainder it means you have to add 5 to the next digit AFTER dividing it by 2.
Examples:
- 1687
- 1/2 = 0, carry 5 because odd
- 6/2 = 3, 3+5 = 8
- 8/2 = 4
- 7/2 = 3, carry 5
- 0/2 = 0, 0+5 = 5
result is: 843.5
This algorithm could be extended to other bases/radices:
Whenever you want to divide a radix B
numeral by d
, and d
is a factor of B
, the extended algorithm can speed-up division. The actual implementation is left as an excercise to the reader ;) lol