# Learning the Binary System

## Tuning What we Have Learned So Far

TTL circuits generally deal with multiplication by adding the number to be multiplied, to itself n number of times. In other words 4 * 9 is adds 4, 9 times. Division is the same way, the circuits just use subtraction instead of addition as division is the inverse of multiplication. Processors can be a little different but usually the compiler will shield you from having to think about these things. This brings us though, to subtraction. Computers do not really need to subtract, only add by an operation called 1's compliment to get a subtracted result. When performing subtraction, the number you are subtracting numbers from is called the minuend. The amount you are subtracting is called the subtrahend.

Now that we have a few of the terms out of the way let's start talking about 1's compliment. 1's compliment is where you turn the 1's in a binary number into 0's and the 0's into 1's, just write the exact inverse of whatever state that column in the number reads. For example 10011010 is 01100101 in 1's complement. To subtract in binary you simply invert the subtrahend and add to the minuend. To better put it, you add the minuend to the 1's complement of the subtrahend. If the sum of the addition adds a digit, we carry this (a 12) to the right-side end. This is called end-around carry here are some examples:

```                                  Remember: When borrowing in n2 (Binary)
It is the same as putting two ones over the borrowing column.
10110              10110
-   01010      OR    + 10101     Remember: To add the end-around carry.
-----------           --------
1100              01011  end-around carry 1
1
-------
1100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

This works because by adding the inverse you are dealing with what's left as a value from 0. If you think about it for a while it will become clearer.

If there is not a 12 to carry, we have a negative result. If you have a negative result, you will need to recomplement the result (invert it). This is important because you will not get the result you are looking for if you subtract in the standard fashion and you get a negative number:

```                10111              10111
-   11010      OR    + 00101
-----------           --------
-1101 Wrong!       11100    Invert
--------
-00011    Correct!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

This happens because the computer (or human) will effectively borrow to infinity. If you want to get around this and still use the standard human method of binary subtraction just make your subtrahend the smallest number and know that that result is a negative.

With the micro-controllers available now, you will rarely need to deal with TTL circuits and the compilers now are so advanced you really will probably not need to deal with 1's complement. You will deal with 2's complement however. 2's complement is insanely easy to explain. The last bit is inverted for negative numbers. That's it, that is all there is to it. This means an 8-bit register that would normally go from 010 (000000002) to 25510 (111111112), would have 7 bits (one less) for a magnitude and one bit reserved for a polarity. Using the 2's complement syntax that register will now describe -12710 (100000002) to 12710 including a 010. The -010 takes a little thinking about, you are not gaining any room, the 0 has the potential to be it's inverse just as the rest of the numbers do. It will become clear with a little pondering.

Endiannism there is an interesting etymology for this word that is beyond the scope of this page. We will however discuss the terms meaning. A packet is a block on information, they can vary in size, for our purposes we will make our packet size 1 byte (8-bits). The Endianness of a packet of information, a byte (anything 8-bits in length) for example is the description of which way the data travels. For instance, in these articles we have been reading binary numbers with the least significant bit on the right. A computer may see things differently, a computer may read information in a little-endian way or a big-endian way. Here is a table to help us:

 Endian First Bit Last Bit Little Least Significant Most Significant Big Most Significant Least Significant

Early Motorola and IBM chips were big-endian and Intel chips were little-endian. This means that the IBM chips interpreted the first bit of an 8-bit variable as the 128 column. In reality there are many different types of endians, for instance with large packets of packets. Another example are systems that treat a 32-bit number as two 16-bit numbers. This really needs to be dealt with on a project to project basis. I mention it here only as a diagnostic step you may want to take if you are having trouble.

Back to Index