0. Sequence
In the last article ** [Talking about binary (I)] **, we spent a huge amount of space, starting from the most basic counting, to understand the principle of various bases, and then through the decomposition and combination of the most familiar decimal system, deduce the conversion process between other bases and decimal system.
This article will delve deeper into the four operations of binary, namely addition, subtraction, multiplication and division, to see how binary and decimal systems differ in computation.
1. Add
1.1 the integer
Addition is usually the first of the four operations mentioned, and it is the extension of the basic act of counting, from adding up one by one, to adding up piles by piles, so that the act of counting is raised a dimension. Following the deduction logic of the previous article, we still start with the decimal system, and first explore the decimal addition.
If we take two decimal numbers, 62 and 185, we can add them up to 247 using simple arithmetic, but how does this work? Let’s think a little bit about what’s going on in your brain, and then look at the vertical expressions you used when you first learned addition as a child:
From the above equation, we can see that the whole process of addition is actually divided into four steps:
- If you add the ones place, you get the one place without carrying;
- If I add the tens place,
6 plus 8 is 14
We’re rounding, because we’re adding the tens place, we’re going to the hundreds place, so we’re going to have the tens place4
In the hundreds place1
; - If you add the hundreds, you only have the hundreds
185
the1
.62
The one hundred is0
, so it is0 plus 1 is 1
; - Finally, add up the results of the previous three steps, and consider any blank space where the number does not exist as
0
And finally we get247
This result.
To summarize the decimal addition process above, it is basically the addition of two digits starting from the lowest place. If there is a carry place, the next digit is added to the result, and the process is repeated until all the digits of the two digits are iterated.
As we mentioned in the last article, the difference between bases is almost exclusively the difference in the way we carry numbers, so if the above addition rules are true for decimal numbers, they are also true for binary numbers. We take two binary numbers 101 and 1110 and add them together:
With 10011, let’s convert the three binaries to decimal: 1110 = 14,101 = 5,10011 = 19, exactly.
When we are familiar with the binary calculation process we can discover, actually the binary addition calculation is more simple than decimal, because the calculation of the lowest binary only three kinds: 1 + 0 = 1, 0, + 0 = 0, 1 + 1 = 10, is much less than the decimal and binary altogether are, after all, only consists of two Numbers 0 and 1.
1.2 floating point number
The above example is the operation of integers, proficient in decimal calculation we know that in decimal calculation, the addition of decimals and integers are almost the same, still from the right to add, carry to the left, until the whole number of digits traversal add, such as: 5.875 + 3.75 = 9.625. This is true in decimal, and binary is no exception. We replace the first two digits in the above formula with binary: 5.875 = 101.111, 3.75 = 11.11, and then add the two binary floating point numbers:
Notice that with decimals, we need to align the decimals. After calculation, 101.111 + 11.11 gives 1001.101 as the decimal number 9.625.
2 subtraction
Now that we’re done with addition, let’s look at subtraction. As we all know, subtraction is the inverse of addition, and subtraction continues to start from the right and work its way to the left. Since this is the inverse of addition, it is important to note that in subtraction, there is no carry, but rather an offplace. 101.111 + 11.11 = 1001.101
The vertical formula above is the process of 1001.101-101.111, each step in the formula I directly calculated the abdication. This one is special, because it’s doing all the displace except for the one on the right, all the way to the left, so the whole vertical looks a little weird. But in general, we get 11.11, which is consistent with the addition mentioned above.
The other thing about subtraction is the case where the subtraction is greater than the minuend, and we know that this is going to be a negative number, and when we do that, we just replace all the bits that don’t exist with zeros, and we just do whatever we want to do, so we don’t expand it anymore.
3 the multiplication
3.1 the integer
From addition and subtraction to multiplication and division, our calculation has raised another dimension, and multiplication is actually the advance of addition. The so-called multiplication is actually a multiple, which is the addition of several several, such as 4 × 3, is actually 4 3 or 3 4.
Let’s start with the decimal system. Take two numbers: 84,57. Let’s see how they work in vertical form:
Well, it looks a lot more complicated than addition and subtraction. But if we look closely, we see that each row of numbers is actually the result of the multiplication of the numbers on the two factors, and the only thing we care about is where we are when we multiply them, so let’s go row by row.
So in the first row, 4 times 7 is 28, and there’s no problem with that, 4 and 7 are both digit numbers, so 10010 to the 0100, so when you multiply them, you’re multiplying two units digits, and the lowest digit is in the ones place. And then the second line, where 56 is 7 times 8, but actually, the 8 is not 8, it’s 80, it’s the tens place, so when you multiply it by 7 in the ones place, you get 560, and we’ve left out the 0. Or to put it another way, the lowest digit of 56 is actually the product of the number of digits of 8 multiplied by the number of digits of 7: 100×101=10110^0\times10^1=10^1100×101=101, so 56 is written in position 10110^1101. If the ones place is the 0th place, then 56’s tens place is the first place.
The 20 on the bottom line is the same as 56 on the top.
The last line, 40, we know from our previous analysis that it’s actually 50 multiplied by 80, with the zeros left out. Times10 ^1=10^2101×101=102, or you can add the exponents 1 + 1= 2, so 40 is written in the second digit from 0.
Finally, add the numbers in the top four rows in order of position, and fill in with zeros where they don’t exist, and you get 4788.
In binary, multiplication in binary is actually much easier than in decimal, for the same reason as addition, because binary has only two numbers, 0 and 1, so there are only three basic multiplications: 0 × 0 = 0, 0 × 1 = 0, 1 × 1 = 1, even the carry is saved, it’s all one digit.
Let’s take two binary integers 110(6) and 101(5) and see what happens when you multiply them:
Since the foundation of binary multiplication is too simple, I didn’t like the decimal here, traverse every number multiplication, but 110 as a whole will be the first factor, the second factor 101 three number apart, to take 110 respectively, so we see the result first line 110, is 1 x 110 = 110, due to 1 on a “0”, So this is also going to be the 0th bit.
And then the second row, 0 times 110 is equal to 000. This 0 is in the 212 to the 121 of 101, which is the first digit, and the lowest digit of 110 as a whole is 202 to the 020, which is the 0th digit, so their product is also written in the 0 + 1 = 0 digit.
And in the third row, 1 times 110 is 110. 1 is in the second place, so I’m going to write 0 + 2 = 2.
Finally, if you add these three numbers together, you get 11110, which is 30, which is exactly 5 times 6.
3.2 floating point number
Now that we’re done multiplying integers, let’s look at multiplying decimals by decimals, and since binary is so easy to calculate, let’s skip decimal and go straight to the binary example. Take two numbers, 1.01 (1.25) and 11.1 (3.5), and look at their vertical calculations:
Here, we also disintegrate the second factor 11.1, calculate the first factor 1.01 as a whole, and get the final result 100.011 (4.375). In this floating-point multiplication, we’re also going to have to pay attention, especially to the bits.
First, in the first row, we see that the lowest order of the first factor that we consider to be a whole is 1.01 in position 2−22^{-2}2−2, which is in position -2, and 0.1 is in position -1. Therefore, the result of the first row is multiplied in position -1 + -2 = -3, so it is all behind the decimal point. And so on, the second and third lines write the least significant bits of the result at -2 and -1, respectively. Finally, add the three results together and fill in the nonexistent positions with zeros to get the final result.
Overall, binary multiplication is much easier than decimal multiplication, and this time we don’t even need to memorize the multiplication table.
4 division
Division is the inverse of multiplication, but it’s a little more complicated than subtraction, the inverse of addition. We’ll start with decimal again, but this time we won’t differentiate between integers and decimals, so we’ll start with floating point numbers.
Take two decimal numbers, dividend 185.65, divisor 2.5, we calculate 185.65÷2.5185.65\div2.5185.65÷2.5:
The numbers are a little big, the whole process is a little long. Here are a few things to note:
-
When dividing with decimals, we are used to making the divisor (2.5) into an integer first, i.e. multiplying both the divisor and dividend by 10n10^n10n, where both numbers are multiplied by 10 before dividing: 185.65×10÷(2.5×10)185.65\times10\div(2.5 × times10)185.65×10÷(2.5×10) 185.65×10÷(2.5×10) Do can be convenient to calculate, the reason is that as we have emphasized many times in the multiplication of digits, because in the process of division also involves the multiplication, the dividend = divisor x quotient, if we remove the divisor of the decimal point, so divisor lowest turned into a “0”, we will be more convenient in calculation, don’t have to worry about figures make a mistake.
-
If the current digit is less than the divisor, the result is 0. Subtract 0 from the current digit and combine it with the next digit (as the higher digit of the next digit) to form a new digit, and then divide by the whole divisor. In the first and second steps above, the result is 0, and then you combine the result of both steps with the next digit, which becomes 185. You can divide by 25,25 times 7 is 175, 185-175 is 10, and then you combine the 10 with the next digit, 6, to get 106. In the procedure section of the above equation, the result of all quotient multiplied by divisor is in red, and the dividend of each new step is in black. We keep doing this until we’re left with 0, and we get the final quotient, 74.26, written above the dividend.
I wrote it a little bit convoluted up here, but if we just kind of remember the decimal division, you can see what’s going on here.
Div10.111.11 ÷10.111.11\div10.111.11÷10.1
The process is the same as in decimal, and we end up with 1.1 (1.5).
At the end of may
In fact, in the original plan, this part (2) also planned to put in the binary bit operation, so as to form a complete operation. However, after learning the lesson that the length of the last article is too long, I think I will not write about operation operation for the moment. In the next article, I will focus on operation operation to save space and improve the reading experience.