MorkaLork Development

Interesting stuff I've picked up over the years...

Binary addition

2009-04-19 21:36:01 | 328 views | binary arithmetics subtraction addition aritmetik math

Binaries and addition



In the base article about binaries we saw how binaries are structured and how they work. This article will show you how to add to binary values and how the math is done.

First, there are a couple of rules to remember when using addition with binaries:

EquationAnswer
0 + 00
0 + 11
1 + 110
1 + 1 + 111


Now, this might seem a bit weird since normaly 1 + 1 = 2, not 10, but these are binaries, and 10 equals decimal 2 (0010). As we can see this logic applies to 1 + 1 + 1 as well, since 0011 equals decimal 3. A picture will explain this:
imagehttp://admin.morkalork.com/uploads/images/binaries/binary9.png

Another way of looking at it is that with binaries you only have ones and zero´s which mean that if a number is more than 1 it gets carried, 1 + 1 = 2, which means that the answer is 0, with 1 carried. This will be illustrated later in this article.

Only the columns with a 1 counts, so in the 0010 we only count the value of the second column to the right, which gives us the sum of 2, and with 0011 we count the rightmost two columns, and the combined sum is 3, so it's logical.

This table of examples will show the logic:

EquationBinariesBinary equation
0 + 00000(0*2^3)+(0*2^2)+(0*2^1)+(0*2^0)
1 + 00001(0*2^3)+(0*2^2)+(0*2^1)+(1*2^0)
1 + 10010(0*2^3)+(0*2^2)+(1*2^1)+(0*2^0)
1 + 1 + 10011(0*2^3)+(0*2^2)+(1*2^1)+(1*2^0)
1 + 1 + 1 + 10100(0*2^3)+(1*2^2)+(0*2^1)+(0*2^0)
1 + 1 + 1 + 1 + 10101(0*2^3)+(1*2^2)+(0*2^1)+(1*2^0)


And so on...
It's vital to have this clear in your mind before you start adding numbers together because there are several ways to think when adding binaries. Either you can go by these rules, which this article will explain how to do, or you can just convert them to regular numbers, add them, and convert them back.

The addening



If we want to add 0001 and 0011 we can use the rules from above to come up with the following calculation:
imagehttp://admin.morkalork.com/uploads/images/binaries/binary10.png
As we can see, counting from the right, we add 1 + 1 and we get the answer 0, with 1 carried. This means that when we get the second column, we add 1 + 0 + 1 and get 0, with 1 carried (again!). Then we proceed to the next column where we add 1 + 0 + 0 and finally get 1, with nothing carried. The last column adds 0 + 0 and ends up with 0. Thus the answer is 0100.

If we want to do this in another way, we can start by converting the binaries to real numbers, this is what we get:
0001 = (0 * 2^3) + (0 * 2^2) + (0 * 2^1) + (1 * 0^0) = 1
0011 = (0 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 0^0) = 3

Then the equation is as follows:
1 + 3 = 4

Converting 4 into bits gives us this:
4 = (0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (0 * 2^0) = 0100

So we get the same answer no matter how we do the calculation, it's just two different ways of solving the problem.

Hopefully this article has helped to understand how to tackle binary adding.


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.