# Binary Tutorial

Binary, base 2, 1's and 0's, whatever you want to call it, is the fundamental basis behind computer logic. It's a series of switches, a simple state of on or off.

When you learned math in school, everything you did was in what is known as Base 10. That means that every digit, each place in a number, can be one of ten possible numbers ranging from 0 through 9. Can you place 12 into a single place? You might be able to 'say' twelve hundred, but write it out and what you actually see is one thousand two hundred. That is, 1 thousand and 2 hundreds. Binary works the same way, only it has a more limited number of available digits that can be used, 0 and 1.

So how do you count in binary? What happens when you count to 10 in normal math? Once you
reach 9, you add a 1 to the tens place and start back at 0 for the ones place. In binary, once
you reach 1, add 1 to place to the left of it and start the current place back at 0. Just as
any place in decimal reaches beyond 9 and you add a 1 to left, so do you the same in binary when
reaching 1. I know what you're thinking, binary is going to be much longer when written out
than its Base10 counter-part. And this is true. But when it comes to computers, this is how all
numbers (and data in general) is seen internally by the system.

The following have exactly the same value in terms of their respective bases:

```
Base10: 138
```

Base2: 10001010

Now I've given you a rough idea of what those 1's and 0's mean, but let's break it down more.

First, I'll show binary counting from 0 to 12 for those of you who may be more visual learners and may recognize the pattern.

```
0: 0000
```

1: 0001

2: 0010

3: 0011

4: 0100

5: 0101

6: 0110

7: 0111

8: 1000

9: 1001

10: 1010

11: 1011

12: 1100

Notice how each 1 moves over to left as the places fill up. Each place further to the left
doubles the number that preceeded it.

Take the number 12 for example in binary:

value | 8 | 4 | 2 | 1 |

bit | 1 | 1 | 0 | 0 |

The numbers about the 1100 show you what each place is equal to in decimal. If there's a 1 in that place then add it, otherwise ignore the number. We have a 1s in the 8 and 4 place so add those two numbers together and you end up with 12.

When talking about computers, a byte represents 8 bits. Each bit is a binary digit. The number
12 in the example above only used 4 bits. For a computer, 12 would be stored as a byte and the extra
4 bits used would just be 0s to left of what we already have. It's like saying 0037 instead of just
37. It means the same thing and the leading 0's don't really mean anything. With 8 bits, that gives
you a maximum number of 255. How do you come up with the maximum number? Each bit has two possible
states, 0 and 1, so two is the base. And there are 8 bits, so the equation is simply 2^{8},
which is 256. Why is the maximum number only 255 then and not 256? Because we start counting from 0,
not 1. Just like in the Base10 system, you can have 0 through 9 which is ten different numbers. To put
this into persepective, let's consider what we could write out with 8 places in plain old Base10 decimal,
99,999,999.

Wow, that's a lot more possibilities than a lousy 256! But what would 99 million look like in
binary when stored on a computer? `101111101011110000011111111`

After looking at that last example, it's easy to see why we don't use binary for every day math. But computers operate by flipping bits on and off and therefore if you plan on becoming a programmer or working with electronics, understandind how those bits fit together is very important. Hopefully you have a better understanding of binary now after reading this.