wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

How do we convert decimal values into binary values?

Converting from decimal to binary to decimal is a very simple process, and follows processes that we use every day.

Suppose, for example, that your best friend calls you and says "I'm coming for a visit and will be there in about 875,000 seconds". Before you have a chance to ask exactly when they will be in, they hang-up the phone.

It is now up to you to figure out when they will be arriving.

 How do we figure this out ??

Well, we know a few things. Namely:

  1. There are 60 seconds in an minute

  2. There are 60 minutes in an hour

  3. There are 24 hours in a day

  4. There are 7 days in a week

  5. There are (approximately) 4 weeks in a year

We would now work backwards to figure how long it would take.

What does this mean ??

It means that your friend will be here in 14,583 minutes and 20 seconds. Notice that the quotient gives the number of minutes, and the remainder gives the number of seconds. We now have to figure out how many hours and minutes that will take (we already know that there are 20 seconds involved). To do that we divide the quotient obtained (14,583) by 60 (the number of minutes in an hour):

We now know that it will take 243 hours and 3 minutes (and, as we saw earlier, 20 seconds) before your friend arrives. Notice that this time, the quotient gives the number of hours and the remainder gives the number of minutes. Now we need to figure out how many days are involved. To do that we divide the quotient obtained (243) by 24 (the number of hours in a day):

We now know that it will take 10 days and 3 hours (and, as we saw earlier, 3 minutes and 20 seconds). Notice that this time, the quotient gives the number of days and the remainder gives the number of hours. Now we need to figure out how many weeks are involved. To do that we divide the quotient obtained (10) by 7 (the number of days in a week):

We now know that your friend will be here in 1 week and 3 days (and 3 hours, 3 minutes, and 20 seconds, as we found out earlier). How many months??

Well, we could keep going, but when the quotient becomes 0, it really doesn't matter. At this point in time we could say that your friend will be here in:

0 Centuries, 0 Decades, 0 Years, 0 Months, 1 week, 3 days, 3 hours, 2 minutes and 20 seconds

But it makes more sense to say your friend will be here in:

1 week, 3 days, 3 hours, 3 minutes and 20 seconds

What does this mean ??

Notice that we obtained the results by keeping track of the remainders, and we reported them in reverse order (we reported weeks first, then days, then hours, then minutes, then seconds; the opposite order in which we obtained them).

What does this have to do with converting decimal values to binary??

The process is exactly the same: We divide a decimal value, keep track of the remainder, and keep dividing the quotient until we get 0 as a quotient.

What do we divide the decimal value by??

Well, let's think about this. If we divide any decimal value by 2, the only possible remainders are 0 or 1, which, conveniently, happen to be the only digits available in binary.

Let's convert the decimal value 13, which we know from the previous tutorial is the binary value 1101.

As we stated earlier, whenever we divide an integer by two, there are only two possible remainders. Notice also that whenever the integer is even (as above, when we divided 6 by 2) the remainder is 0; whenever the integer is odd (as above, when we divided 13 by 2, 3 by 2, or 1 by 2) the remainder is 1. Because we are collecting from last to first, we also know that if the decimal integer we are converting is even, the right-most bit will be '0'; if the decimal integer we are converting is odd, the right-most bit will be '1'. In this case, since the integer 13 is odd, the right-most bit is '1' (as we saw above).

Let's try another simple conversion. We already know that the decimal value 10 is equal to the binary value 10102 (from the previous tutorial). We're going to use alternative notation (to save space):

Once again, notice also that whenever the integer is even (as above, when we divided 10 by 2 or 2 by 2) the remainder is 0; whenever the integer is odd (as above, when we divided 5 by 2 or 1 by 2) the remainder is 1. This time, since the integer 10 is even, the right-most bit is '0'.

Let's try one more example. Let's convert the decimal value 157 to binary:

How do we know that this is the correct answer ??

Well, we know some things:

  1. To represent 157 (actually, 158) we need 8 bits (since 27 = 128 and 28 = 256), which is the number of bits it took us, and

  2. Since 157 is an odd number, the right-most bit must be '1', which is the what the right-most bit in our result is.

But we still don't know that this is the correct answer !!
Isn't there some way of checking our result ??

Yes -- We will get to that in an upcoming tutorial.

Additional References:

  1. http://www.geocities.com/regia_me/decToBin.htm This site also contains interactive conversion problems.

  2. http://www.goshen.edu/compsci/mis200/decbinary.htm  This site has an alternative method of conversion that subtracts powers of 2 from the decimal number to be converted.

  3.  http://www.southwest.com.au/~jfuller/binary/binary5.htm   Another good site on how to use the alternative method for counting in Binary, 

At this point in time, you should be able to Answer the following questions:

1.  The number 73 requires ___ bits in binary:

a.   6                                                          d.   9
b.   7                                                          e.   10
c.   8

2.  The number 37 (decimal) in binary would be:

a.   11011                                                  d.   11101
b.   10111                                                  e.   11001
c.   10101

3.  How do we represent numeric values from 0 to 10 in Binary (Assume we are using 8-bits)?

4. If you were asked to convert 4,203 inches to yards, feet and inches, how would you do it?

5. If you were asked to convert 52,897,605 seconds to years, months, days, hours and seconds (assume 30 days per month), how would you do it?

6.  Convert the following decimal numbers to binary.

a.   34
b.   63
c.   128

7.  Without actually converting the numbers 84, 137, and 693 into binary, what can we predict about how the numbers will appear in binary?

Answers

  1. B

  2. A

  3. 0     =    00000000
                +               1
                -----------------
    1      =    00000001
                +                1
                -----------------
    2      =    00000010
                +                1
                -----------------
    3      =    00000011
                +                1
                -----------------
    4      =    00000100
                +                1
                -----------------
    5      =    00000101
                +                1
                -----------------
    6      =    00000110
                +                1
                -----------------
    7      =    00000111
                +                1
                -----------------
    8      =    00001000
                +                1
                -----------------
    9      =    00001001
                +                1
                ----------------
    10    =    00001010

4.        We know:

There are three feet in a yard
There are 12 inches in a foot

Therefore, we should first find out how many feet there are (the remainder of this calculation will tell us how many inches remain) 

Next we should determine how many yards there are (the remainder will tell us how many feet remain)

NOTE: We are collecting the information in REVERSE order

4203 % 12   3        <= The number of inches remaining

4203 / 12      = 350   <= This tells us the total number of feet in 4,203 inches

350 % 3        2        <= The Number of feet remaining

350 / 3          = 116    <= The number of yards

So there are 116 yards, 2 feet, 3 inches in 4,203 inches

5.        We know that there are:

60 seconds in a minute
60 minutes in an hour
24 hours in a day
30 days in a month
12 months in a year

Again, we must collect in reverse order:

52,897,605 % 60    45           <= The (remaining) number of seconds
52,897,605 / 60       =  881626  <= The total number of minutes
881626 % 60           46           <= The (remaining) number of minutes
881626 / 60             =  14693     <= The total number of hours
14693 % 24             =   5             <= The (remaining) number of hours
14693 / 24               =   612        <= The total number of days
612 % 30                 12           <= The (remaining) number of days
612 / 30                    =  20           <= The total number of months
20 % 12                    =   8             <= The (remaining) number of months
20 / 12                      =   1             <= The number of years

52,897,605 seconds is 1 year, 8 months, 12 days, 5 hours, 46 minutes and 45 seconds

5.       

2 | 34 à 0
2 | 17
à 1
2 |   8
à 0
2 |   4
à
2 |   2
à 0
2 |   1
à 1    Collecting from bottom to top we have: 1000102

2 | 63
à 1
2 | 31
à 1
2 | 15
à 1
2 |   7
à 1
2 |   3
à 1
2 |   1
à 1    Collecting from bottom to top we have: 1111112


2 | 128
à 0
2 |   64
à 0
2 |   32
à 0
2 |   16
à 0
2 |     8
à 0
2 |     4
à 0
2 |     2
à 0
2 |     1
à 1    Collecting from bottom to top we have: 100000002

This page was last updated on 05/26/05