This article is part of a series, access the series index.
All About Binary
So we’ve covered counting in binary, converting binary to octal or hexadecimal, basic boolean algebra and logic gates, and sequential encoding using Gray codes with binary… there’s one thing about binary that I can think of right now that’s left. At least one thing I can be sure I can explain. There’s other things involving binary that are beyond my skill to explain but that I’ve included at the end of this article as links to Wikipedia.
Math in Binary
Okay, computers process binary information as input and output. What do they do in their processing? Much of it involves sending the electrical charges around many different circuits, but much of it also involves basic binary math. Addition, subtraction, multiplication, and division. You can do those in binary? Oh yes, yes you can.
Yes, you can even do fractions! However, it’s a little different because you’re actually doing binary past a decimal point. Binary numbers past the decimal point progress in negative powers. Meaning: 2^-1, 2^-2, 2^-3, 2^-4, 2^-5, etc.
This doesn’t make as much sense until you see that 2^-1 = 1/2 as we know it. 1/4 = 2^-2 = .01 (binary). But, 1/5 is impossible. The closest you can get is .0011… repeating forever, 0.001100110011…. This is true even if in decimal you’re able to get 0.2.
This is due to the very nature of the binary system, that being that it is base 2. You can only get fractional binary numbers to not be never ending when the denominator of the fraction is a power of 2. All other fractions are infinite and usually approximations.
Addition is actually considered the easiest operation to do in binary. The first thing you have to know about is ‘carrying’. You do carrying all the time when you add two numbers in decimal together. Whenever two numbers added together exceed the value of the base you ‘carry’ a ‘digit’. It’s so natural to us we don’t really think about it, but for example 5 + 5 = 10. See, in terms of the ones place 5 + 5 is greater than the maximum of what it can hold: 9. So you put a zero in the ones place and put a 1 in the ten’s place, as that’s the next actual value, 10.
Same thing with 8 + 8 = 16. 8 and 8 together are greater than the maximum the one’s place can hold (9), so we carry a 1 to the ten’s place (10) and then place 6 more into the one’s place to create the new value 16. Makes you really think about counting doesn’t it?
Same thing in binary. Think of it like a modified AND logic gate, or disjunction operation. 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10. Normally 1 + 1 would create 1, but since we’re adding and carrying a bit, the carried bit ends up in the 2’s place. So in essence we have 0, 1, and 10 (2).
I have a piece of code/diagram that may help illustrate the concept further:
10110 10010 + 10110 -------- = 101000
Subtraction is very similar. In subtraction we end up borrowing a digit instead of carrying one. Borrowing is an inversion of the carrying method, you end up borrowing a ‘1’ from the next place value.
The ‘mapping’ if you will of subtraction thus follows: 0 – 0 = 0, 0 – 1 = 1 borrow 1, 1 – 0 = 1, and 1 – 1 = 0. When you borrow from the next place value you are essentially subtracting a 1 from it (the next place value), which is a subtraction operation in itself.
I have another piece of code/diagram that may help illustrate this concept further:
- -- - 1011010101 - 101110010 ----------- = 101100011
Although there is a mapping for multiplication: 0 * 0 = 0, 0 * 1 = 0, 1 * 0 = 0, and 1 * 1 = 1. It’s pretty obvious but it makes sure you’re keeping it straight when you are computing it out. Writing it out is very much like extended addition. For every 1 in the bottom multiplicand we have a row repeating the top multiplicand. For every 0 we have a row of zeros. So, it’s a series of additions with the top multiplicand in different positions. That hopefully doesn’t sound too confusing but just in case I have a code/diagram that may help illustrate this concept:
010 x 101 ----- 010 + 000 + 010 ------- 01010
Division is probably the hardest one to quite get, at least for me, but it’s attainable. Division is of course a mirror of normal division. There is no particular mapping to division because the operation is performed longhand and involves subtraction. Since you should already be familiar with subtraction I’ll just cut to the operation itself. First you have a divisor that you are ‘putting into’ a dividend. To do so, you take so many bits at a time of the dividend equal to the number of bits of the divisor, then if the divisor can be subtracted from those set of bits you put a 1 in the quotient. Repeat this process until you have a full quotient and a remainder in the bottom. Again I have a code/diagram that may help to illustrate my narrative:
1001 --------- 110 ) 0111010 - 110 ----- 0010 - 0000 ------ 101 - 000 ----- 1010 - 110 ------ 100
As you can see binary arithmetic calculations are easy-peesy. I mean you only have to digits to worry about: 1 and 0. I remember learning my multiplication tables and 7 was the hardest for me to remember. I was never good at 7 until I was an adult. But, as a kid, I thought I had to know it all now… I couldn’t imagine myself as an adult. Here are some further links about binary you might find interesting:
I hope you’ve learned something new with this tutorial!
If you appreciate my tutorials please help support me through my Patreon.