CSE 111, Fall 2004

Algorithm: Convert Decimal D to 8-Bit Binary B

Last Update: 8 September 2004

Note: NEW or UPDATED material is highlighted


  1. Input D;

  2. Write down the binary places with their decimal values below them:

    ___   ___   ___   ___   ___   ___   ___   ___
    128   64   32   16   8   4   2   1

    1. Find all the decimal values (in each place of the binary numeral) that add up to D;
    2. Write 1's in all those places;
    3. Write 0's in all the other places;

  3. Let B = the resulting binary numeral;

  4. Output B;

  5. Stop.

Test this with:
  1. D = 255
  2. D = 129
  3. D = 128
  4. D = 87
  5. D = 64
  6. D = 2
  7. D = 1

Questions to think about:
  1. How do you do step 3a? Can you write another algorithm to explain it in more detail?
  2. What happens if D = 0? Does the algorithm need to be modified for this case?
  3. What happens if D = 256?



Copyright © 2004 by William J. Rapaport (rapaport@cse.buffalo.edu)
file: 111F04/dec2bin-alg2-2004-09-08.html