**ALGORITHM:**

Decimal_to_Binary (Decimal_number,

Quotient, Binary, J)

STEP 1: [INITIALIZE] SET I=1

STEP 2: [INITIALIZE] SET Quotient = Decimal_number

STEP 3: Repeat step 4 and 5 WHILE Quotient >=1

STEP 4: SET Binary[I+1] = Quotient % 2

STEP 5: SET Quotient = Quotient / 2

STEP 6: Repeat step 7 FOR J = I -1 TO 1

SET J = J – 1

[END OF LOOP]

STEP 7: PRINT “ Binary [J] ”

STEP 8: EXIT

**DISCUSSION:**

Converting from decimal to binary notation is slightly more difficult conceptually, but can easily be done once you know how through the use of algorithms. Begin by thinking of a few examples. We can easily see that the number 3= 2+1. and that this is equivalent to (1*2^1)+(1*2^0). This translates into putting a "1" in the 2^1 column and a "1" in the 2^0 column, to get "11". Almost as intuitive is the number 5: it is obviously 4+1, which is the same as saying [(2*2) +1], or 2^2+1. This can also be written as [(1*2^2)+(1*2^0)]. Looking at this in columns,

2^2 | 2^1 | 2^0

1 0 1

or 101. What we're doing here is finding the largest power of two within the number (2^2=4 is the largest power of 2 in 5), subtracting that from the number (5-4=1), and finding the largest power of 2 in the remainder (2^0=1 is the largest power of 2 in 1). Then we just put this into columns. This process continues until we have a remainder of 0. Let's take a look at how it works. We know that:

2^0=1

2^1=2

2^2=4

2^3=8

2^4=16

2^5=32

2^6=64

2^7=128

and so on. To convert the decimal number 75 to binary, we would find the largest power of 2 less than 75, which is 64. Thus, we would put a 1 in the 2^6 column, and subtract 64 from 75, giving us 11. The largest power of 2 in 11 is 8, or 2^3. Put 1 in the 2^3 column, and 0 in 2^4 and 2^5. Subtract 8 from 11 to get 3. Put 1 in the 2^1 column, 0 in 2^2, and subtract 2 from 3. We're left with 1, which goes in 2^0, and we subtract one to get zero. Thus, our number is 1001011.

## No comments:

## Post a Comment