All computational operations in a computer are calculated in the binary numeral system by the processor. Therefore, operating with binary numbers is an important skill for programmers, circuit engineers and other people concerned. Programmers use binary calculations to speed up execution of programs, to work with hash tables, to process encoded information, to calculate addresses in computer memory, etc. Most often, in their work they have to deal with the operations of addition and subtraction.
Binary addition
Let's learn how to add binary numbers by hand. In fact, this information might not be new to you. Most likely, you already know the algorithm for adding numbers using the column addition method. It also applies to binary numbers. Let's take a closer look at how to do this.
1. Preparation
You need to determine which of the two numbers is the largest. Write down the larger number first, and then below it write the second number, aligning it to the right. Now draw a line below to record the answer under it. For example, let's try adding numbers and . We will write them like this:
2. Calculations
When writing a decimal number, we write the number of units at the first place from the right, the number of tens at the second place from the right, the number of hundreds at the third place from the right, and so on. For a binary number, the same holds true. But instead of tens we have twos, instead of hundreds we have fours and so on. Calculations start from units, moving from right to left. We can also pad the leading digits of numbers with zeros for convenience.
Use the following addition rules:
Note that is in decimal. As you can see, all addition rules are the same as for the decimal system. Now let's solve this example: . In the first place of both numbers are zeros, so we add to , which is . We move on to the next place and add to , which is 1. Then we need to add to , which will be . And the answer is . You can see an illustration of this addition below:
Now let's solve another example: . On the first step, we need to calculate . According to the rules, we get . But how to write this to the answer? We would do the same in the case of decimal numbers: we would carry to the next digit. For example, . Solving using the column addition method, we would add to , which is , and carry to the next digit and get . Why does this work? Let's represent as the sum of and group them by units, tens and hundreds, and add 1. Then it would look like this: . This is the same as . We moved inside the next brackets, to the numbers of the same order. The same holds true for binary numbers. Let's take a look at the example we considered above: .
When carrying, we write down above the next digit.
Let's try solve the example , now that we know all the rules. The first step is to add to and get . Therefore, we transfer to the next place.
Then we need to solve again, but from the previous step we've carried , so we need to add . Therefore, we write down at this place and carry to the next place.
Further, we are add , but from the previous step we've carried . Thus, we need to add . Now we transfer to the higher order digit again.
The last step is to sum . We will get , but we have carried from the previous step. We get , and since this is the highest order digit in the number, we just write . Then the answer is .
We have received an answer . Let's check it. The decimal equivalent of is 11 and the decimal equivalent of is 7. Therefore, the result is 18. The binary equivalent of 18 is . That's right!
Now try to solve an example by yourself. The answer should be . You can compare your solution with the solution below.
Now you've learned how to add binary numbers!
Binary subtraction
Let's figure out how to subtract binary numbers manually without using a calculator or software. First, how would you subtract two large decimal numbers? Most likely, you would write them one below the other and subtract bit by bit, starting from the last digit. This method is usually called column subtraction. If you know this method, then good news for you, you can use it for binary numbers as well.
Surely, an example will help to understand how manual binary subtraction works. Let's subtract from . Here, the number 214 is represented in binary as and the number 58 is represented as .
1. Preparation
You need to determine which of the two numbers is the largest. The largest number is called the minuend, and we write it first. The smaller number is called the subtrahend, and we write it under the minuend with right alignment. Then we draw a line under the subtrahend, and below it we will write the answer to the expression.
2. Calculations
Start subtracting from the right. We need to subtract the digits of the numbers, moving from the lowest place value to the highest. To do this, follow the binary subtraction rules to subtract the numbers:
Back to the example: let 's try to calculate the expression . In both the minuend and the subtrahend numbers, the digit 0 is in the lower place. So the first step is to subtract (0 - 0), which is equal to 0. Similarly, we move on to the next higher order digit and subtract (1 - 1), which is 0. In the next step, we have to subtract (1 - 0), which is 1.
Now the expression (0 - 1) is in the queue, but how to subtract from 0? What do we do in this case while subtracting decimal numbers? You may remember that we need to borrow 1 from the next digit in order, which in decimal would mean subtracting the required digit from 10 instead of 0. For example, if we subtract 3 from 120 by the column method, we have to borrow 1 from 2. We subtract 3 from 10 instead of 0 and get 7 in the first place of the resulting number. Therefore, the result of subtracting (120 - 3) is 117. Why does it work? Let's transform into the sum . When we borrow 1 from 2, in fact, we take 10 from the second bracket and move it into the bracket with the number 3. After borrowing, we get .
Below you can watch the animation illustrating the principle of borrowing in detail. For simplicity, when we borrow, we will write -1 on top of the next digit, which means we need to subtract 1 from this digit.
As you can see, place values of any neighboring digits differ by 10 times. Therefore, you can borrow at any stage of the calculations. Everything is the same in the case of binary numbers. We need to borrow 1 from the next digit and subtract from 10. Do not forget that 10 in the binary system of calculus is equal to 2. But the algorithm remains exactly the same. You can see this for yourself with the example of .
And now, let's go back to our main expression: at this stage, we need to subtract 1 from 0. Recall that we subtract from and have already calculated 3 digits of the answer. After borrowing, we subtract , which is 1.
In the next step, we have to subtract 1 from 0, due to past borrowing . This means we need to borrow 1 from the next digit again. Therefore, we get .
The next higher order digit has (0 - 1), for which we need to borrow 1. Do not forget that we borrowed at the last stage, too, and we need to subtract 1. Hence, we get (10 - 1) -1 = 0.
There are no digits left in the subtrahend. Therefore, we assume that 0 is subtracted from all other digits of the minuend. We borrowed from 1, so . Next, .
We have received the answer . Let's check it. The decimal equivalent of is 214 and the decimal equivalent of is 58. Therefore, the result is 156. The binary equivalent of 156 is . Look at us!
Finally, let's look at an expression . What is it equal to? At each step, you need to borrow.
The answer is .
Now you know how to subtract binary numbers!
Conclusion
Addition and subtraction of binary numbers are performed similarly to addition and subtraction of decimal numbers, using the column addition method. You need to write the numbers in a column. Next, start the calculations from unit places. Follow the rules of addition and subtraction to calculate each digit of the resulting number. Pay particular attention to the carry rule for addition and the borrow rule for subtraction. Applying the column method step by step, you will get the answer.