Binary Arithmetic

In the previous article, we looked into the different numbering systems. But going forward we would be focusing only on Binary numbers and operations on them.

Let’s start with basic arithmetic on binary numbers; addition and subtraction. But, as usual, let’s first try to lay down the rules using the decimal numbering system.
OK, this will be 3rd-grade maths, but please bear with me as we will need these rules to understand how to perform the same operations on binary numbers.


Addition :

Let’s look at a few simple decimal additions.

We know how to do it :
1. Place both the numbers one below the other, with ones place-values aligned
2. Start with the first number and increment for a number of times equal to the second number.

And we get 5 + 1 + 1 = 7
Addition of 5 and 2

We know how to do it :
1. Place both the numbers one below the other, with ones place-values aligned
2. Start with the first number and increment for a number of times equal to the second number.

And we get 5 + 1 + 1 = 7

Addition of 25 and 12

Same as above, just with two digits :
1. Place both the numbers one below the other, with ones place-values aligned
2. Do the single digit additions on both the numbers individually

And we get 25 + 12 = 37

Addition of 125 and 97

Now we have a different case than the previous two.
1. We again place the numbers one below the other, with one's place-values aligned.
2. When an addition of two digits in the same place-value is more than the max single digit value, which is 9 for decimal, in other words, the result of the addition is a 2 digit number, then, we keep the digit in the in the ones place and carry over the other to the next place-value.

Let's take the values one-by-one, starting from the ones place.
5 + 7 = 12 -> We keep 2, carry over 1
2 + 9 + 1 carried over = 12 -> We keep 2, carry over 1
1 + 1 carried over = 2 -> We keep 2

So, we get the result 222

OBSERVATIONS :

1. We always start the addition from the ones place ( right to left )
2. If the addition of the digits in the current place-value is over the base value, we keep the digit in the ones place and carry over the rest to the next place-value.

Let’s apply these observations to the Binary numbers.

We know that Binary Numbering System is Base-2
So, any number above 1, should be treated for carry-over.
In decimal 1 + 1 = 2
In Binary 2 = 10
When we get 10, we keep 0 and carry over 1

Lets do the addition, starting from the ones place.
1 + 1 = 10 -> Keep 0, carry over 1
0 + 0 + 1 carried over = 1 -> Keep 1,
1 + 1 = 10 -> Keep 0, carry over 1
0 + 1 + 1 carried over = 10 -> Keep 0, carry over 1
1 + _ + 1 carried over = 10 -> Keep 0, carry over 1
_ + _ + 1 carried over = 1 -> Keep 1

So, we get 100010

Simple, isn’t it?

Let’s now do some subtraction.


Subtraction

Again, let’s start with a decimal subtraction, but let’s dive straight into 3 digit subtractions.

Here we are trying to subtract 49 from 357.
Let's check it step-by-step, again, starting from the ones place.
And we immediately see a problem. We cannot subtract 9 from 7, because 9 is larger than 7.
So, as we know, the solution is to borrow a 10s from the next place-value. 
Note, when we borrow, we borrow a value equal to the Base-Value of the number system. Hence, in Base-10 we always borrow the next 10s value.

Let's start again.
7 - 9
Cannot subtract, so borrow a 10 from the neighbor
7 becomes 17, and 5 becomes 4, in reality, 50 becomes 40
17 - 9 = 8
4  - 4 = 0
3 - _ = 3
So, we get 308

OBSERVATIONS :

1. We always start from the ones place ( right to left )
2. If the top digit is smaller, we borrow a value from the neighbor, which is equal to the Base-Value of the number system
3. The neighbor which lends, loses a value equal to it's place-value

OK, let’s apply it to the Binary numbers.

Again here, a binary number system means Base-2.
So, when we borrow we always borrow a 2, which means 10 in binary.
Let's start...
1 - 1 = 0
0 - 1
Cannot subtract, borrow 10
0 becomes 10 and 1 becomes 0
10 - 1 = 1
0 - 0 = 0
0 - 1
Cannot subtract, borrow 10
0 becomes 10 and 1 becomes 0
10 - 1 = 1

And, we get 1010

I had told you, Easy as pie!

So now, we are ready for some real stuff!

In the next article, we will look into the One’s and Two’s Complement.