4.3. Binary Conversions - Multiplication/Division Method¶
The biggest issue with the “table method” shown on the previous page is that it is hard to translate into an algorithm that a machine could run. The multiplication/division methods shown on this page are much easier to express in simple terms as they rely on mathematics more than things like “look at the table for _______”.
4.3.1. Decimal to Binary¶
The following algorithm converts a decimal number to a binary one:
Step 1: Start with a blank
answerand thenumberyou are converting Step 2: Divide yournumberby 2 to make aquotientand aremainderStep 3: Place yourremainderon the left side of youranswerStep 4: If yourquotientis 0, you are done Otherwise, make thequotientyour newnumberand go back to step 2
Here is an example of running the algorithm to convert \({11}_{10}\) to binary:
Animation used by permission of Virginia Tech
So the decimal number 11 in binary is \({1011}_{2}\).
4.3.2. Binary to Decimal¶
To convert a binary number to its decimal value, we can use almost the same trick, but in reverse:
Step 1: Start with the
numberyour are converting and theanswerof 0 Step 2: Multiply youranswerby 2 Step 3: Remove the leftmost digit ofnumberand add it to youranswerStep 4: Ifnumberhas no more digits, you are done Otherwise, go back to step 2
If we follow this algorithm to convert \({1101}_{2}\) into a decimal value, it would look like this:
Step 1:
numberis 1101 andansweris 0 Step 2:answeris multiplied by 2 - it becomes 0 Step 3: Remove leftmost digit ofnumberand add toanswernumberis now 101 andansweris 1 Step 4:numberstill has digits, go back to step 2 Step 2:answeris multiplied by 2 - it becomes 2 Step 3: Remove leftmost digit ofnumberand add toanswernumberis now 01 andansweris 3 Step 4:numberstill has digits, go back to step 2 Step 2:answeris multiplied by 2 - it becomes 6 Step 3: Remove leftmost digit ofnumberand add toanswernumberis now 1 andansweris 6 Step 4:numberstill has digits, go back to step 2 Step 2:answeris multiplied by 2 - it becomes 12 Step 3: Remove leftmost digit ofnumberand add toanswernumberis now "" (empty) andansweris 13 Step 4:numberis empty. We are done, the answer is 13.
As mentioned earlier, the advantage of this algorithm is that it very easily converts into relatively simple computer code. To demonstrate that, the algorithm is implemented in Python in the codelens below. You DO NOT need to worry about exactly how the Python language works. You will notice that turning the English algorithm above into code requires some changes, but the code shown follows the same process described above. See the “tips” box below the code lens for help running the program.
Activity: CodeLens Convert "1101" to decimal (datarepresentation_binaryconversions21)
Tip
How to use the “codelens”
You can click the “forward” button to watch the code run line by line.
The blue area that appears below the buttons will show you the current values of
numberandanswer.Use can use the large scroll bar to scroll the code to the right - at the end of each line of code is an explanation of what that line “says”
Self Check
- 5
- You are dividing by 2 until you reach 0. 5 divisions won't get you there
- 4
- You are dividing by 2 until you reach 0. 4 divisions won't get you there
- 6
- 49
- You are dividing by 2 until you reach 0. don't need that many divisions
If you use the division method to convert 49 to binary, how many times do you have to do step 2?
