So yeah, it may have been interesting to you that binary can represent any number by using only 0s or 1s, but what you may not have realized looking at it, kind of like Roman numerals, is that binary can be added, subtracted and even multiplied just like you find in everyday Base-10. So what's the difference? Why bother utilizing one over the other? Well, don't forget, computers think in binary, not Base-10, and this is also how they perform functions, which is done through binary mathematics. But why do we need to know it? Well, you don't really, totally need it. But the thing is, as a future programmer, in order to make your programs fluid and in order to pick out mistakes, you need to think like a

*computer*.

This is one of the problems for people who have studied programming for a good portion of their lives. They know the concepts and tricks inside and out, but they often have great difficulty explaining it to others. Why? Well, it's quite simple. First off, they take too much for granted that's already become second nature to them but

*not*to you, and second, more importantly, they've become so accustomed to thinking like a computer that their brains have officially died and made human language no longer a viable option for communication. The thing is, when you learn enough about programming, it becomes almost impossible to explain it to someone that doesn't because you've become so engrossed in computer thought it becomes incredibly difficult to translate it into normal, understandable speech, not to mention the fact that you have to explain everything anyway to get anywhere. So our next step in learning how to program is binary addition. This way, you'll start to learn to think like a computer. Later on, this will make programming ten times easier, and you'll be quite impressed with how well terms and techniques come to you afterwards. However, I should state at the outset that we're not going to be focusing on multiplication at all. Why not?

Because for our purposes, it's useless. The reason is that there's a problem with the Master System's CPU. The CPU is simply that part of the SMS' hardware (or any computer, for that matter) that contains most or all of the functions so it can perform all the programs (games, etc.) that are thrown at it. We'll talk about the CPU in more detail later. So you have what you may know as a microchip (more commonly referred to as an integrated circuit or IC), which is just a little piece of of metal and plastic that enables the CPU to function when electrical impulses (binary) are thrown around. The Master System's CPU is a Z80. We'll be learning all about the Z80 and how it works later, but for now the main idea here is that the Z80 has no ability to multiply or divide, it can

*only*add or subtract. And since we're working with a Z80, we need to know how it thinks. To do that, we need to understand how it adds and subtracts in binary.

You already have the basic knowledge to do it, it looks much more difficult at first than it really is. In fact, in a way, it's a heck of a lot easier than Base-10 because you're only working with 0s and 1s, you don't have to combine them to get 3 or 5 or 789 as you do in Base-10, all answers will only be in binary, and thus only use 0s and 1s. The first thing we need to get out of the way is how we're going to represent our numbers. Remember how in your binary exercises you had representations such as 00 that you were supposed to convert into Base-10? Well, technically, this is incorrect. Yes, it still yields the same number, but let's compare the following.

**00**

**00000000**

Hmmmm, is there really a difference there? I mean, they both mean 0 in Base-10, so what's the problem? The problem is that the Z80, our CPU, is an 8-Bit microprocessor (CPU can also be referred to as MPU, or microprocessing unit). What do 8 bits give us again? One byte. The information you're going to be throwing around is going to be 8 bits long, or one byte. Thus, we need to start writing our representations as they should appear in a full byte. So even though 00 and 00000000 are both 0 in Base-10, the latter is the correct way to write it because that's the way the Z80 processes data. So how should we write 01? 00000001. That's it. Always keep in mind that we're working with 8 bits, or one byte, so you need to write our numbers like that from now on. Now then, on to the next step.

There are only four rules you need to learn to understand binary math. They are as follows:

**0 + 0 = 0**

0 + 1 = 1

1 + 0 = 1

1 + 1 = (1) 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = (1) 0

The first three are straightforward, the fourth has something in it that needs to be commented on. Whenever you add 1 and 1 in binary math, the answer is 0. However, there is also a carry. The carry means that you simply take 1 and carry it over to the next column. This is the same as you do with Base-10. Thus:

**1**

88

+ 22

110

88

+ 22

110

I purposely put that 1 up there so you recognize what's happening. What's happening? Well, 2 and 8 yields us 10 in Base-10, but you can't just write 10 below the line, you carry the one over and this becomes added to the next line. Yes, I know you know math, but I want you to

*see*it. And of course, in this example, it just so happens the carry is 1, but you all know that it can be anything up to 9 in Base-10. So we do the same thing when we work with binary addition, except that we

*never*get a carry higher than 1. Let's look at an example of basic binary math before we get to the issue of the carry.

**00000000**

+ 00000001

00000001

+ 00000001

00000001

Hmmm, that's a little too easy for us, let's work with a higher set of numbers, because in Base-10, 0 + 1 also equals 1. Look at this instead:

**00001010**

+ 00000101

00001111

+ 00000101

00001111

Now that's a bit more juicy. Remembering what we learned in the binary tutorial, what we see up there is the following in Base-10:

**10**

+ 5

15

+ 5

15

Using the rules we learned about binary addition above, the result comes pretty easily to us at this point. But what about that carry issue I mentioned? How does that come into play? This throws a little more difficulty into the realm of binary math, so we need to take a step further to turn our brains into computers and think like the Master System.

Next Page