Difference between revisions of "CSC103: DT's Notes 1"
Line 144: | Line 144: | ||
Let's put the numbers we've generated in decimal and in binary next to each other: | Let's put the numbers we've generated in decimal and in binary next to each other: | ||
− | decima | + | decima binary |
000 00000 | 000 00000 | ||
001 00001 | 001 00001 | ||
Line 190: | Line 190: | ||
Let's try that for the binary number 11001. The base is 2 in this case, so the value is computed as: | Let's try that for the binary number 11001. The base is 2 in this case, so the value is computed as: | ||
− | + | 11001 = 1 x 2<sup>4</sup> + 1 x 2<sup>3</sup> + 0 x 2<sup>2</sup> + 0 x 2<sup>1</sup> + 1 x 2<sup>0</sup> | |
− | + | ||
− | + | = 1 x 16 + 1 x 8 + 0 x 4 + 0 x 2 + 1 x 1 | |
− | + | = 16 + 8 + 0 + 0 + 1 | |
− | + | = 25 in decimal | |
Line 212: | Line 212: | ||
------------ | ------------ | ||
− | It's tempting to just go ahead and find the answer, isn't it? Instead let's do it a different way and concentrate only on 3 + 5 | + | It's tempting to just go ahead and find the answer, isn't it? Instead let's do it a different way and concentrate only on 3 + 5. For this we write down the list of all available digits, in increasing order of weights. For the purpose of this section, we'll call this list of digits the ''table of available digits''. |
0 | 0 | ||
Line 232: | Line 232: | ||
<font color="magenta">8</font> | <font color="magenta">8</font> | ||
− | We move on the next column and add 3 + 8. | + | We move on the next column and add 3 + 8. Using our ''table of digits'', this is what we get: |
0 | 0 | ||
Line 246: | Line 246: | ||
----- | ----- | ||
1 0 +7 | 1 0 +7 | ||
− | <font color=" | + | <font color="red">1</font> <font color="magenta">1</font> +8 <--- the result is 8, but with a roll-over |
− | Here we had to roll over our list of available digits. We know the rule! When we roll over we add 1 to the digit on the left. In this case the digit'''s''' on the left. We'll put this new digit ( | + | Here we had to roll over our list of available digits. We know the rule! When we roll over we add 1 to the digit on the left. In this case the digit'''s''' on the left. We'll put this new digit (red) above the third column of numbers as follows: |
− | <font color=" | + | <font color="red">1</font> |
1 3 3 | 1 3 3 | ||
+ 3 8 5 | + 3 8 5 | ||
Line 277: | Line 277: | ||
<font color="magenta">5</font> 1 8 | <font color="magenta">5</font> 1 8 | ||
+ | If we find ourselves in the case where adding the leftmost digits creates a roll-over, we simply pad the numbers with 0 and we find the logic answer. | ||
+ | |||
+ | So we have a new rule to add numbers in decimal, but actually one that should hold with any base system: | ||
+ | <tanbox> | ||
+ | We align the numbers one above the other. We start with rightmost column of digits. We take the first digit in the column and start with it in the ''table of digits.'' We then go down the table a number of steps equivalent to the digit we have to add to that number. Which ever digit we end up on in the table that's the digit we put down as the answer. If we have to roll-over when we go down the table, then we put a 1 in the column to the left. We then apply the rule to the next column of digits to the left, until we have processed all the columns. | ||
+ | </tanbox> | ||
+ | |||
+ | Ready to try this in binary? Let's take two binary numbers and add them: | ||
+ | |||
+ | 1010011 | ||
+ | + 0010110 | ||
+ | ------------ | ||
+ | |||
+ | Our ''table of digits'' is now simpler: | ||
+ | |||
+ | 0 | ||
+ | 1 | ||
+ | |||
+ | Here we start with the leftmost column, start at 1 in the table and go down by 0. The result is 1. We don't move in the table: | ||
+ | |||
+ | 1010011 | ||
+ | + 0010110 | ||
+ | ------------ | ||
+ | 0 | ||
+ | Done with the rightmost column. We move to the next column and add 1 to 1 (don't think too fast and assume it's 2! 2 does not exist in our table of digits!) | ||
+ | 0 | ||
+ | 1 <---- we start here, and go down by 1 | ||
+ | ------ | ||
+ | <font color="red">1</font> <font color="magenta">0</font> +1 | ||
+ | We had to roll-over in the table. Therefore we take a 1 and add it to the column on the left. | ||
+ | |||
+ | <font color="red">1</font> | ||
+ | 1010011 | ||
+ | + 0010110 | ||
+ | ------------ | ||
+ | <font color="magenta">0</font>0 | ||
Revision as of 11:23, 31 January 2012
--© D. Thiebaut 08:10, 30 January 2012 (EST)