Programming Assignment #4

Key learning: loops

Write a program that will convert an unsigned integer into binary representation. The algorithm for converting an integer into binary is explained and shown below in an example:

1. Take the number and divide it by 2

2. If the result has a remainder, add a bit to the binary result.
This first time around you add a bit to the 2^{0} position. The
second time around you add to the 2^{1} position, the third time around
you add to the 2^{2} and so forth...

3. Now remove the remainder from the result in (2) and set that number equal to the new number.

4. Repeat (1)-(3) until result from part (3) is 0.

Ex. | Number is 5 | Current binary representation | |

1. | Divide number by 2. | 5/2 = 2.5 | |

2. | Remainder of 2.5 = 0.5. | Remainder is nonzero => Set 2^0 position to 1 | 1 |

3. | Remove remainder | set new number to 2.0 | |

4. | Divide new number by 2. | 2.0/2 = 1.0 | |

5. | Remainder of 1.0 = 0. | Remainder is 0 => set 2^1 position to 0. | 01 |

6. | Remove remainder, | set new number to 1.0 | |

7. | Divide new number by 2. | 1.0/2 = 0.5 | |

8. | Remainder of 0.5 = 0.5. | Remainder is nonzero => set 2^2 position to 1 | 101 |

9. | Remove remainder, | set new number to 0 | |

10. | New number is equal to 0 | quit | |

Result: 101 |

What you need to do to write this program is

(1) use floating point registers

(2) figure out out to check a number for its remainder

(3) reset the new number

(4) add the correctly weighted bit to the result.

Good luck.