MathComputational mathNumeral systems

Binary arithmetic

4 minutes read

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 101121011_2 and 1112111_2. We will write them like this:

Preparation for addition

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.

The leading digits of the numbers are padded with zeros

Use the following addition rules:

0+0=00+1=11+0=11+1=1020+0=0 \\ 0+ 1 = 1 \\ 1+0 = 1 \\ 1+ 1= 10_2 \\

Note that 10210_2 is 22 in decimal. As you can see, all addition rules are the same as for the decimal system. Now let's solve this example: 1002+102100_2 +10_2. In the first place of both numbers are zeros, so we add 00 to 00, which is 00. We move on to the next place and add 00 to 11, which is 1. Then we need to add 11 to 00, which will be 11. And the answer is 1102110_2. You can see an illustration of this addition below:

Additional step by step

Now let's solve another example: 1012+1101_2+1. On the first step, we need to calculate 1+11+1. According to the rules, we get 10210_2. But how to write this to the answer? We would do the same in the case of decimal numbers: we would carry 11 to the next digit. For example, 129+1=130129 +1 = 130. Solving using the column addition method, we would add 99 to 11, which is 1010, and carry 11 to the next digit and get 130130. Why does this work? Let's represent 129129 as the sum of 100+10+10+9100 + 10 + 10+ 9 and group them by units, tens and hundreds, and add 1. Then it would look like this: (100)+(10+10)+(9+1)(100) + (10 + 10) + (9 + 1). This is the same as (100)+(10+10+10)+(0)(100)+ (10 +10 +10) +(0). We moved 1010 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: 1012+1101_2 + 1.

Example of binary number addition

When carrying, we write down +1+1 above the next digit.

Сarrying when adding

Let's try solve the example 10112+11121011_2+111_2, now that we know all the rules. The first step is to add 11 to 11and get 10210_2. Therefore, we transfer 11 to the next place.

Addition step one

Then we need to solve 1+11+1 again, but from the previous step we've carried 11, so we need to add 1+1+1=102+1=1121+1+1 = 10_2 + 1= 11_2. Therefore, we write down 11 at this place and carry 11 to the next place.

Addition step two

Further, we are add 0+10+1, but from the previous step we've carried 11. Thus, we need to add 0+1+1=1020 + 1 + 1 = 10_2. Now we transfer 11 to the higher order digit again.

Addition step three

The last step is to sum 1+01+0. We will get 11, but we have carried 11 from the previous step. We get 10210_2, and since this is the highest order digit in the number, we just write 10210_2. Then the answer is 10010210010_2.

Last step

We have received an answer 10010210010_2. Let's check it. The decimal equivalent of 10112{1011_2} is 11 and the decimal equivalent of 1112{111_2} is 7. Therefore, the result is 18. The binary equivalent of 18 is 10010210010_2. That's right!

Now try to solve an example 1000111002+1110102100011100_2 +111010_2 by yourself. The answer should be 1010101102101010110_2. You can compare your solution with the solution below.

Another example of addition

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 1110102{111010_2} from 110101102{ 11010110_2}. Here, the number 214 is represented in binary as 110101102{ 11010110_2} and the number 58 is represented as 1110102{111010_2}.

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.

Binary  subtraction

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:

00=011=010=11021=1{0-0 =0} \\ {1 - 1= 0} \\ {1 -0 =1} \\ {10_2 -1 = 1}

Back to the example: let 's try to calculate the expression 1101011021110102{ 11010110_2 - 111010_2}. 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.

Binary subtraction step by step

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 1203120 -3 into the sum (100)+(10+10)+(03){(100) + (10 + 10) + (0 - 3)}. 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 (100)+(10)+(103){(100) + (10) + ( 10 - 3)}.

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.

Subtraction

Decimal digits

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 11021{110_2 -1}.

Decimal digits (2)

And now, let's go back to our main expression: at this stage, we need to subtract 1 from 0. Recall that we subtract 1110102{111010_2} from 110101102{11010110_2} and have already calculated 3 digits of the answer. After borrowing, we subtract (1021){(10_2 - 1 )}, which is 1.

Example of subtraction

In the next step, we have to subtract 1 from 0, due to past borrowing (11)=0{(1 -1) = 0}. This means we need to borrow 1 from the next digit again. Therefore, we get (11)+1021=1{(1-1)+10_2 -1 = 1}.

Example of subtraction (2)

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.

Example of subtraction (3)

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 (11)0=0{(1- 1) -0 = 0}. Next, 10=1{1-0 =1}.

Example of subtraction (4)

We have received the answer 100111002{10011100_2}. Let's check it. The decimal equivalent of 110101102{11010110_2} is 214 and the decimal equivalent of 1110102{111010_2} is 58. Therefore, the result is 156. The binary equivalent of 156 is 100111002{10011100_2}. Look at us!

Finally, let's look at an expression 1000021{ 10000_2 -1}. What is it equal to? At each step, you need to borrow.

Another example for subtraction

The answer is 11112{1111_2}.

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.

173 learners liked this piece of theory. 13 didn't like it. What about you?
Report a typo