EXAMPLES

Examples of converting between decimal and binary...


10110012->  8910

Using the algorithm:
   1 x <- dk   (move leftmost digit to x)
   2 i <- k-1 (set index to next digit)
   3 while i > 0 do
   4     x <- di +2x (compute inner product)
   5     i <- i - 1 (decrement index, end while loop)
   6 stop

 

x <- 1
x <- 0+1*2 = 2
x <- 1+2*2 = 5
x<- 1+2*5 = 11
x <- 0+2*11 = 22
x <- 0+2*22 = 44
x <- 1+2*44 = 89

 

Using the "place value equation":

10110012 = 1·26 + 0·25 + 1·24 + 1·23 + 0·22 + 0·21 + 1·20 =
1·64 + 0·32 + 1·16 + 1·8 + 0·4 + 0·2 + 1·1 =
64 + 0 + 0 + 16 + 8 + 1 = 8910


49710->  1111100012

Using the algorithm:
   1 s <- 0 (starting from rightmost digit)
   2 while x != 0 do
   3     ds <- x mod 2 (get remainder of divide by 2)
   4     x <- floor(x/2) (reset x to quotient)
   5     s <- s + 1 (set up for next digit)
   6 stop

497/2 = 248 + 1
248/2 = 124 + 0
124/2 = 62 + 0
62/2 = 31 + 0
31/2 = 15 + 1
15/2 = 7 + 1
7/2 = 3 + 1
3/2 = 1 + 1
1/2 = 0 + 1
The writing the remainders bottom to top we get:
answer = 111110001


Using the "place value equation":

497 = 1·256 + 241 = 1·28 + 241
241 = 1·128 + 113 = 1·27 + 113
113 = 1·64 + 49 = 1·26 + 49
49 = 1·32 + 17 = 1·25 + 17
17 = 1·16 + 1 = 1·24 + 1
(several "won't go" conditions to produce 0s)
1 = 1·1 + 0 = 1·20 + 0
Write the place-value parameters top to bottom
answer = 111110001

[Prev][TOC][Next]

CS1104 Main Page
Last Updated 2001/02/05
© L.Heath, 2000, modified by J.A.N. Lee