Jump to content
Banner by ~ Ice Princess Silky
  • entries
    10
  • comments
    99
  • views
    2,332

Binary Math (Adding Ones and Zeroes)


Splashee

2,423 views

When programming games for a computer, either it being a game console, or a PC, it is very handy to understand how binary numbers work. The Ones (1) and Zeroes (0) that control the computer's brain, the CPU, as well as the bits stored in memory or on physical media.
While bits are patterns of Ones and Zeroes, and can represent anything such a text, or graphics, it is also used to store and calculate numbers. These numbers are stored in binary form, and the CPU knows how to read them. We humans can read them too:

 

11001011011 is binary for the number 1627. The colors I have chosen is blue for binary, and purple for decimal number (the numbers we humans count with). Reading binary is easy! We have to step though each digit at a time, starting right and move towards left until all bits have been processed. For each step (digit position) we take, we must add a number to our final result. The final result will be in decimal form, and starts at 0 when we begin translating:

The first digit (to the very right) in the binary number is worth exactly 1 decimal. If that digit is 1, then we must add 1 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The second digit in the binary number is worth exactly (1 + 1) decimal (which is 2 decimal). If that digit is 1, then we must add 2 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The third digit in the binary number is worth exactly (2 + 2) decimal (which is 4 decimal). If that digit is 1, then we must add 4 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.
The fourth digit in the binary number is worth exactly (4 + 4) decimal (which is 8 decimal). If that digit is 1, then we must add 8 to the final result. If it is 0, then we will skip it. Then we move to the next digit to the left of the previous one.

Have you notice a pattern? Each digit to the left of the current one's position is worth 2 times as much! That's because binary is base-2. In our decimal system, which is base-10, each digit to the left is worth 10 times that of the current digit's position.

 

So to read the value 11001011011, we have to go through each digit's position and add how much they are worth, but only if the digit is 1, and not when it is 0! We begin with the final result being 0:

First digit is 1, so the result is 1...
Second digit is 1, so 2 is added to the result making the result total 3 so far...
Third digit is 0, so skip (else you would have added 4 to the result)...
Fourth digit is 1, so 8 is added to the result making the result total 11 so far...
Fifth digit is 1, so 16 is added to the result making the result total 27 so far...
And next digit is 0 so skip (else you would have added 32 to the result)...
And next digit is 1, so add 64 to the result making the total 91 so far...
And next digit is 0, so skip (else + 128)...
And next digit is 0, so skip (else + 256)...
And next digit is 1, so add 512 to the result, total 603...
And the last digit is 1, so add 1024, and the final total result is............................... 1627.

 

Adding two binary numbers together is done in using the same rules as adding decimal numbers, (by hand) easiest done with the columnar addition approach. Remember that carrying over to the next digit's column is done when an overflow occurs, such as 1 + 1 or (1 carry) + 1 + 1, just similar to how decimal addition works, 9 + 1 or (1 carry, which is worth 10) + 9 + 1. So if you didn't get that last part, it probably doesn't matter as you might be using a different format. The rules of the math is the same though!

Let's do an example. Let's add the number 2 and 1 together, so that the sum is 3. That is easy since you don't need columnar addition... However, in binary, those numbers are 10 and 1 and 11 respectively. So we should/can use columnar addition:

 10
+ 1
___
 11

First column, 0 and 1 is added to make 1. In the second column, 1 is added to nothing, or 0 if you may, and the final sum is 11.

 

Let's do another example:

 101101
+ 10010
_______
 111111

Pretty straight forward. But the numbers here are quite interesting. Because we are using binary instead of decimal, we don't need to use any carry! But the binary number 101101 is the decimal number 45, the binary value 10010 is the decimal number 18, and the binary number 111111 is the decimal number 63, a carry is required when doing this calculation in decimal form:

(1)   <- Carry
 __
 45
+18
___
 63

 

Carrying is done in the same way with binary numbers:

(1)
 __
  1
+ 1
___
 10

First column, 1 and 1 is added to make 10, but since that number can be stored as a single digit, the digit 1 is carried over to the next column to be added next. In the second column, 1 (the carry) is added to nothing, or 0 if you may, and the final sum is 10.

Of course, there is the case where we must add 1 and 1, as well as a carry (1), and the sum would then be 11. Then 1 is stored in the column, and the other 1 is carried over to the next:

(11)
____
  11
+ 11
____
 110

 

And that concludes addition of binary numbers. Next up is subtraction, however, it is not as you may think. See you next time!

  • Brohoof 3

41 Comments


Recommended Comments



4 minutes ago, Pentium100 said:

Just for clarification, "1/11" in my previous post was used as an example of decimal recurring fraction, one divided by eleven.

But it works in binary too (1/3 decimal is by definition 1/11 in binary :D)

  • Brohoof 2
Link to comment

I learned in school that you indicate a repeating part of a decimal fraction with parenthesis (1/30 = 0.0(3) ), though I guess just like division on paper is slightly different in different countries based on what @Brony Number 42 wrote, maybe the repeating fraction is marked differently as well.

2 minutes ago, CypherHoof said:

But it works in binary too (1/3 decimal is by definition 1/11 in binary :D)

Yes :twismile:. I chose that as an example of more than one digit repeating - 1/3 = 0.(3), but 1/11 = 0.(09) - to show that this is how I mark the repeating part. 

By the way, just look at us nerds, discussing math :twismile:.

Edited by Pentium100
  • Brohoof 2
Link to comment

@Pentium100 I had seen repeating decimals with a bar over those digits. Maybe we should talk about base 16 and how it is easy to take a binary number and group the digits 4 at a time and easily make a hex number.

@Splashee yes doing math in any base uses the same steps. I find it easy to not try to convert to decimal when I think about it.

 

  • Brohoof 1
Link to comment
37 minutes ago, Pentium100 said:

By the way, just look at us nerds, discussing math :twismile:.

Started by a person that doesn't really care that much for math, more the practical use of it. I am really happy to see you explore the parts I couldn't explain myself.

  • Brohoof 1
Link to comment
8 minutes ago, CypherHoof said:

i think it is more ironic that you looking to a Pentium for maths :D

Pentium never had bugs. Never! I refuse to believe thee were any faults with Pentium CPUs.

  • Brohoof 1
Link to comment
2 hours ago, CypherHoof said:

i think it is more ironic that you looking to a Pentium for maths :D

Especially floating point :twismile:

  • Brohoof 1
Link to comment
4 hours ago, Brony Number 42 said:

Maybe we should talk about base 16 and how it is easy to take a binary number and group the digits 4 at a time and easily make a hex number.

Or take three bits at a time and make an octal number, like they are used in aircraft transponders and Linux file permissions (chmod 750...). But hex is better, since a byte fits in exactly two hex digits.

  • Brohoof 1
Link to comment

Then we will basically get into ascii characters. Like how easy it is to just add hex 20 to each number, to get the character. It's kinda basic.

 

I will write a new blog entry about binary subtraction as soon as this one dies, lol. I am expecting it to fall of the forum index any time now :wacko:

  • Brohoof 1
Link to comment

You could also explain it through vectors and give the already asked for multiplication table to let them guys know. Actually, let me try to make a table and see how it goes with your first example, splashers.

Potences (2 power n times)    10      9      8      7    6    5      4    3    2    1    0
"solved" value of potence    1024    512    256    128  64   32     16    8    4    2    1
Za example numbah!             1      1      0      0    1    0      1    1    0    1    1
Result ya'll add             1024    512     0      0   64    0     16    8    0    2    1

You need some basic algebra knowledge to understand the table.

Pretty much, the first row is what power is 2 at for the column. That means, 2 multiplied by itself how many times. I'm not entirely sure (or more likely too lazy to) why any number at power 0 equals 1, but it's a loooong weird mathematical explanation through I don't recall what theorems and that's unpractical for the use of our table.
So, 2^0 = 1, 2^1 = 2, 2^2 = 2x2 = 4, 2^3 = 2x2x2 = 8 and so on. You get the idea and that's how you explain the second row I put there.

I must add an important note on this table. Just as if you were reading any other number, the digit at the far right will always be the one with the lower value, and as it gets farther from the right the value will be bigger.

The third row is the number in question in the binary example splashers gave us.In binary numeration, you can have only 0 and 1, and nothing bigger than that on a digit.

The 4th row is the result of multiplying the 2nd and 3rd rows.

Finally, all we need to do is add up each cell from the 4th row to know the value of that.
Let's do it!

1024 + 512 + 0 + 0 + 64 + 0 +16 + 8 + 2 + 1 = 1627

1627 = 1627

Is not that hard to understand now, is it?

Edited by Jesse Terrence
  • Brohoof 2
Link to comment
On 1/31/2020 at 1:23 PM, Brony Number 42 said:

Pure math means theoretical math. Pencil and paper. Humans doing math. Not how computers do math. 

You can have binary division, something like 1001/110 and get an answer with decimals. I know how to do that on paper, but I don't know exactly how a computer does it.

AHHH!!!

hehehehehe

Allow me to explain how does a computer do it.

Pretty much, given it works on binary and binary is rather hard to give exact results when using decimal numbers, it uses a loop until it reaches an "acceptable" result, near the result it should probably give.

It solves operations through numerical analysis.

For that .23 example from waaaaay before, the computer would need to reach a number slightly bellow .23 to convert it to binary, but will limit how many iterations it will do until it decides the result is acceptable. So how does it do it?

Well, the algorithm goes step by step on the binary numeration, comparing it's result with the queried number and deciding if it should add a number before the next step or not. For instance, it would go "is .1 bin (.5 dec) bigger than .23 dec?". Given it is bigger, the computer does not add 1 after the point, and goes to the next step. "Is .01 bin (.25 dec) bigger than .23 dec?". It is bigger, so it leaves another zero. Next step "is .001 bin (.125 dec) bigger than .23?" Answer is no, so it takes that 1 and goes to the next step. "Is .0011 bin bigger than .23?" and so on until it complies with a preset rule on how much can its result deviate from the actual result. Let's say it might give it 0.001 deviation lower a green light and once the deviation is just that low it will take whatever result was as good.

EDIT: Now, regarding division, it does work like pretty much anyone works a division, but in binary. The algorithm I described above has something to do here, as well.

First, we need to understand what do we do to solve a division in the practical way.

1.- We compare if the value we are trying to divide is bigger or equal to the number we shall divide it by.

2.- If the value is bigger or equal, we substract the number we are dividing by the one we shall divide it by, else, we take a step to the right (and add zeroes if we are already in decimals).

3.- If we have a remainder after substracting, we compare that remainder with the number we are dividing it by and repeat the proccess.

4.- If there's no remainder, we have the final result, else, we decide where to stop dividing once the values we keep getting do not represent a percentage that could affect later calculations.

 

The truth is, the same goes with binary operations and computers to that matter.

Gimme a few hours and I shall make an example of division with small values.

Edited by Jesse Terrence
  • Brohoof 1
Link to comment

@Jesse Terrence thanks for that, it makes sense. Iterative proccesses are tight! Also, the reason 2^0 = 1 is if you consider this fraction as an example

2^3 / 2^3 = 1 obviously, but also = 2*2*2/(2*2*2) 

You know to subtract the exponents when dividing things raised to a power.

x^m/x^n = x^(m-n) 

So if m=n then we get 1. However, 0^0 is not allowed.

  • Brohoof 1
Link to comment
8 minutes ago, Brony Number 42 said:

@Jesse Terrence thanks for that, it makes sense. Iterative proccesses are tight! Also, the reason 2^0 = 1 is if you consider this fraction as an example

2^3 / 2^3 = 1 obviously, but also = 2*2*2/(2*2*2) 

You know to subtract the exponents when dividing things raised to a power.

x^m/x^n = x^(m-n) 

So if m=n then we get 1. However, 0^0 is not allowed.

@Brony Number 42 Sweet! Straight to the point and clear as water explanation. Thank you!

Now, Going back to the division thing, to be able to divide we need to know how to substract first. Substraction is a bit "tricky" if we try to aproach it the way we usually do it with decimal numeration, but you'll see how easy it turns out and actually it makes sense they prefer to use binary over decimal for computer processes.

We'll take an easy substraction for my explanation: 1 0 0 1 - 1 0 0 (9 - 4 in decimal).

1.PNG.c7e485983d38da0a4828036de8c63494.PNG

I know, I'm not explaining much right now, but I'll get to it now.

As you can see, the result is the same. Just as if we were doing a common substraction, we substract the number bellow from the number on top of it.

So, doing it step by step...

2.PNG.8b21bef62982c176d12510c9865c400a.PNG

As you can see, everything goes nice and dandy until we get those cases in which we are substracting more than what we have on top! (NOTE: That triangle thing is an exclamation mark of mine, don't mind it please).

That's the confusing part, because we were taught in school that we "borrow" 1 from the number to the left and then substract as if it was a 2 digits number instead of a 1 digit one. So we relate that little marking right away to "10" instead of the "2" it represents in reality.

Let's have a look at it:

Captura.PNG.b9b8b2e902487437ab3557bc36554a38.PNG

If you notice, I highlighted that "borrowed" one in red. In reality, it doesn't read "10" as we are acostumed to, but instead, it is 1 0, which would be 2 in decimal:

We proceed to substract, and we'll end up with 1.

5.PNG.88c96878e59c8535d750d1d40731b570.PNG

Lastly, we proceed with the last column. Originally, it would be read as follows:

6.PNG.f4f33afc22b7dd122a82db073b907dd9.PNG

The empty space implies a 0, just like usual. However, if you remember, in the other column we "borrowed" 1 to perform the operation and get a positive value. Thus, that "borrowed" 1 should be payed on the next column, making our implied 0 a 1, or what is the same substracting it from the 1 that lend it to us.

So, after we finished all the operations, our result is as follows:

7.PNG.5953995442d8323759384c8a077f2a7b.PNG

as you noticed, I put the borrowed ghost 1 in lyra green-blueish to remind you we are supposed to substract it, though it doesn't exist in the initial operation.

So, our result was 0 1 0 1, which equals 0 4 0 1. We add them up and we have 5. 5 equals 5.

I'll take a break and continued in a few minutes.

 

  • Brohoof 2
Link to comment

Now, as for division, we have the basics ready and thus, we are ready to bite the big bad boy.

I'll do 2 examples. The first one, with whole numbers, and the next one with nasty decimal numbers.

For my first magic trick, we'll divide 21 by 7. But in binary, of course!

So, 21 would  1 0 1 0 1, and 7 would be 1 1 1. Our operation then would be 1 0 1 0 1 / 1 1 1.

Or...

378182996_div1.PNG.92bceebecfc575ba5bbb163a6c90143e.PNG

I'll use the "house" thingie because it's easier to visualize what we're doin' with it.

As I commented earlier, division is pretty much an algorithm we repeat. Compare, substract or add a digit, rinse and repeat.

Knowing that, we don't need to see the thing as anything else but that, the same steps over and over. Let's get started, then!

first, we shall compare the 3 first numbers with our number outside the house.

860915422_div2.PNG.75f7cab9a728ea595f3f989df92b1cf5.PNG

As you can see, 1 1 1 is obviously bigger than 1 0 1, so we take a step to the right and compare again!

492583634_div3.PNG.573ce13a50df96f485567a4554cfc095.PNG

Yes, it is!

Now we need to substract!

We shall encounter a "tricky" situation we don't usually see in the decimal system, since the unit we borrow doesn't usually make the next number grow a whole "step", to say.

div4.PNG.97d97236a5e5560cc86595f55e274c7f.PNG

What happens there is that that green one will add to the 1 on the bottom, making it go from 0 1 to 1 0. That means it will add 1 to the one to the left of it, and thus, making another 0 1 become 1 0 instead. Pretty much, after that first green one appears, we should make the addition until we don't get our values modified and continue:

1823017420_div5.PNG.73ad0b9a676c26b53c7fac03b28b0f9a.PNG

Kinda hard to explain through words, but I hope you get the idea. That one we borrowed before causes a "chain reaction" and by the end of the chain we end up with 1 0 0 to be substracted from 1 0 1.

42376942_div6.PNG.bb4a81d9896f476282eb992382154505.PNG

Color coded for your easier understanding (I hope it works).

Anyway, after we get that remainder, we continue just as any other substraction. We move a step to the right.

113251289_div7.PNG.7394197c431512c3559e6376bd441581.PNG

And there. The result is up there.

1 1, which would be 2 + 1 = 3.

21 divided by 7 equals 3.

3 equals 3. The first division is done.

Now, for our second case, the one that get's actually ugly, we'll divide 6 by 9, which in binary would be 1 1 0 divided by 1 0 0 1:

94888988_dev1.PNG.9b730bfec296a58cc0fb067171847e86.PNG

it would look like that through the house technique thingie. As you can tell from just looking at it, it's gonna be a division with decimal point:

1517329580_dev2.PNG.c0e2bdddd00225ed1ac30c749a351ed3.PNG

There. As you can see, once we add that zero after moving a step to the right, we can tell now we can substract and thus, we proceed with the substraction:

1775868908_dev3.PNG.22286bdd121143c8a21b399efbdb71bc.PNG

As you can see, the substraction works just as usual, I didn't get into too much detail this time because I think you probably might have got a hang on how it's done with binaries. either way, the reminder becomes 0 0 1 1, and thus, we add a zero to move a step to the right as in any division:

402419760_dev4.PNG.d0185258a14cc7fffb2a5af48a465fd7.PNG

As you can tell, 1 0 0 1 is bigger than 1 1 0... but a pattern comes to the eye too!

1 1 0 was the number we started with, so, the result will be an irrational number that will keep repeating that pattern.

1577272915_dev5.PNG.6ef023d1ff4814d41f54a8219b2fbb25.PNG

The result will be a periodic 0.101010...

It will keep repeating to approach the result we get in decimal numeration, which would be 0.66...

Hope this helps. Anyway, I'm sure splashers can add even more to this topic, and I'm eager to read all the things he might add later on.

Edited by Jesse Terrence
  • Brohoof 2
Link to comment

@Jesse Terrence Exactly. One issue I have is comparing the number to its decimal form. I never do that unless I need to. The logic is the same in any base. I never look at 10 bin and say "two." I say "ten" or "one zero" because there is no "two."

  • Brohoof 2
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Join the herd!

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...