CIS3355:
Business Data Structures |
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:
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:
But we still don't know that this is
the correct answer !! Yes -- We will get to that in an upcoming tutorial. Additional References:
At this point in time, you should be able to Answer the following questions: 1. The number 73 requires ___ bits in binary:
2. The number 37 (decimal) in binary would be:
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 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
4. We know:
There are three feet in a
yard 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 Again, we must collect in reverse order: 52,897,605 % 60
= 45 <= The
(remaining) number of seconds 52,897,605 seconds is 1 year, 8 months, 12 days, 5 hours, 46 minutes and 45 seconds 5.
|