# Understanding Binary: Math

Posted by Jared Kipe on | 0 Comments

Tags:

Though not as important as understanding binary in general, looking at some simple math in binary will allow you to conceptualize some of the operations that you will be working with on a regular basis.

In base 10, addition is trivial until you need to awkwardly overflow to the next digit, that is to "carry".  For example, if you add 1 to 9, you need to add a digit, and "carry" a 1 into the next digit.

The same thing happens in binary, but unfortunately, it happens a lot more frequently.  Since a digit is only 1 or 0, there is a roughly 50% probability that you will need to "carry" to the next digit.  Worse yet, you will often find yourself needing to carry more than one time... Example, in base 10, adding 1 to 99, you add 1 to 9, carry one into the next digit and end up adding 1 to 9 again, carrying again to come out to 100.

You can always add in stages, so if you need to add 121 to 99, you can break it up into 121+9, and then whatever that is you add it to 90.  When you think about doing it on pencil and paper you typically break it into stages based on digits.

121+9 = 130

130+90 = 220

1001b + 1b = 1010b

1010b + 10b = 1100b

## Subtraction

Just like addition, but you need to "borrow" if you don't have enough in the first number to subtract.

Honestly, subtraction is trickier to do and I'm trying to keep this pretty brief and general so I'm going to skip over the real details here.  But if you'd like to learn more about it, here are some binary subtraction examples.  (Note that when you borrow 1b from the higher digit you end up with 10b in the lower digit i.e. 2)

## Multiplication

Do you like adding binary numbers?  Want to do it as many times as you have digits in the smallest of the two products?

#### Multiply 1001b by 101b

1001
x101

001001
000000
100100

101101b

That probably needs some explanation. Basically, just like base 10 multiplication, you break the whole multiplication operation up into 3 simple multiplication processes and 3 additions. Every time you multiply a number by 0, it is 0, and by 1 is the original number. The multiplications are truly trivial. If its a 1 in the digit in question you copy down the top number and shift it over as many digits as you have already multiplied. (I added bolding around the actual number we wrote down, and padded all the numbers with zeroes to keep everything lined up.)

### Special Case: multiplying by 2 (10b)

In base 10, the easiest number (besides 1 or 0) to multiply by is by 10.  Multiplying by 10 is something most people can do in their heads without even thinking about it.  And if you were looking at the number on a computer you'd probably just copy, paste, and add a 0 to the end.

Multiplying by 2 in base 2 is exactly the same thing.  You just "shift" the number to the left one digit and add a 0.

#### Multiply 1110 1101 0101b by 10b

0001 1101 1010 1010b (that was so easy it took me longer to "rebalance" the 4 digit spaces, than to even consider the answer)

### Whats with the groups of four binary digits?

Well, just like in decimal numbers we often put a comma (or period) after every 3 digits, you often see groups of 4 binary numbers grouped.  We will be going over this notation and the reasons when we go over hexadecimal numbers.

## Division

Division gets messy and is similar in this way to subtraction, if you want to learn the nitty gritty, here is an example algorithm on binary division.

### Special Case: dividing by 2 (10b)

As you may have guesses, this is the opposite of that neat multiply by 2 trick we just went over.  With 1 simple exception...

Right "shifting" the number is only perfect if the number is even, that is to say that it doesn't end in 1.  This is just like if a number ends in any number but 0 and you divide it by ten there will be some decimal fraction remaining.

1111 0100b

#### Question

Don't get discouraged if a lot of this is going over your head. I'd doubt any of it is required to be a great programmer. If anything, as you go on, certain aspects of programming will probably jog your memory about this post.

Continue on to Understanding Binary: Hexadecimal