CSE 111, Fall 2004
Algorithm: Convert Decimal D to 8-Bit Binary B
Last Update: 8 September 2004
Note:
or
material is highlighted
Input D;
Write down the binary places with their
decimal
values below them:
___
___
___
___
___
___
___
___
128
64
32
16
8
4
2
1
Find all the
decimal
values (in each place of the
binary
numeral) that add up to D;
Write 1's in all those places;
Write 0's in all the other places;
Let B = the resulting binary numeral;
Output B;
Stop.
Test this with:
D = 255
D = 129
D = 128
D = 87
D = 64
D = 2
D = 1
Questions to think about:
How do you do step 3a? Can you write another algorithm to explain it in more detail?
What happens if D = 0? Does the algorithm need to be modified for this case?
What happens if D = 256?
Copyright © 2004 by
William J. Rapaport
(
rapaport@cse.buffalo.edu
)
file: 111F04/dec2bin-alg2-2004-09-08.html