# Binary Math

Why do we need to learn binary math? The reason stems from the use of digital signals. Digital signals have two states, high-voltage and low-voltage, and if implemented well, they have perfect fidelity, at least with simple straight-forward digital signalling (SERDES is another story…). That means they deliver their values from driver to receiver without any mistakes…absolutely zero mistakes. As you have learned, digital signal states can represent the two logic states, and certain configurations of logic gates can perform math on those binary input values. So, this is why computers at the fundamental level use binary numbers, because they can take advantage of the perfect fidelity of digital signalling, and because logical circuits and processes exist to perform just about any kind of math or logical operation we want on binary numbers.

Binary addition follows the same process as decimal addition. Let’s go through an example in decimal.

```      54      +73      ---      127
```

1st you would add the 4 to the 3 resulting in 7. Since this is less than 10, there is no carry value and you put 7 in the 1s spot of the answer. Next, you would add 5 to 7 resulting in 12. Since this is more than 10, you would put 2 in the 10s spot and carry 1 to the next position. There are just zeros in the next position, so the carried 1 is added to zero and the result is 127.

Note that the operation of carrying a 1 to the next position represents 10 of the previous position. Another way to think of this is 5+7 in the 10s position is 12 in the 10s position which is 120, but you can’t represent this in the 10s position, so you carry 10 10s to the next position, which is 1 100s, and put down the 2 10s that are left in the 10s position. Binary addition works the same way, except each position represents powers of 2 rather than powers of 10, and carrying a 1 to the next position represents 2 in the previous position. Binary example:

`      011010      +010110      -------       110000`

Start at the right most position (1s position). 0+0 is zero resulting in 0 in 1s position. Then, move to the 2s position where 1+1 is 2. 2 is too much, so you have to carry 1 to the next position, which represents 2 in the current position leaving you with 0 in 2s. Next is the 4s position where 1+0+carried 1 is 2, which is again over the limit and must be carried leaving 0 in 4s position. 8s position is the same story with 1+0+carried 1 being 2, which needs to be carried leaving 0 in 8s position. 16s position has 1+1+carried 1 = 3. So, you carry 2 to 32s position leaving 1 behind in 16s position. 32s position is 0+0+carried 1 = 1. You are done, and the result is 110000b.

Fun, right? Well, perhaps not. One thing that is more fun than this manual process is using a calculator or computer program to add binary for you.

## Binary Multiplication

The manual process of binary multiplication is just like the process for decimal numbers. In fact, it is easier, because you never have to carry. Why is that? It is because all the possible multiplications in a position are 0*0=0, 1*0=0, 0*1=0, and 1*1=1. It is not possible to get a result that is higher than 1, so carrying values is never needed.

```      111      *110    ---------       000     +1110    +11100    ---------    101010
```

Just like with decimal numbers start with the 1s position of the bottom number and multiply that times each position of the upper number. 0*anything is 0, so put down 000. Shift down a line and add a 0 to the right side of the result. Then, multiply the 2s position of the second number times each position of the top number. 1 times a number is that number, so put down 111. Then, shift down a line and add 2 zeros to the right side of the result and multiply the 4s position of the bottom number times each position of the top number: 111. Then, perform binary addition on the 3 result lines to get the answer, which is 101010b.

There is an interesting thing that happens when you multiply a binary number by 2 or a power of 2. The result is that number shifted to the left by a number of positions equal to the power of 2 that you are multiplying it by. So, 1001b multiplied by 100b (4 in decimal) is 100100b. 1001b got shifted to positions to the left. Division has the opposite effect. The result of devision by 4 is the number shifted two positions to the right. This trick can be taken advantage of in some situations for speed and efficiency.

## Negative Numbers

I am going to skip division of binary numbers, because it is again the same process as for decimal numbers, and I think you can figure it out. Besides, who the heck does long division by hand these days? Only small-minded math teachers, they are the only ones who do that.

I am also going to skip subtraction, since that is also the same process as for decimal numbers. However, subtraction does bring up an important subject for the binary numbering system, and that is negative numbers. For decimal numbers we use the unary sign operator – as in -8 to indicate a negative number. It is also fine to do this for binary numbers: -10110b, for example. But, there is a trick that is very helpful for computers, and that is called Two’s Compliment form.

## Two’s Compliment Form

You can represent a negative binary number in Two’s Compliment form, which has some interesting and helpful properties. To do this, write the positive version of the number in binary, invert each bit, and then add 1. For example, let’s represent -27 in 2’s compliment form.

Write in Binary: 00011011b

Flip each bit: 11100100b

Add 1: 11100101b – This is the Two’s Compliment form of -27.

2’s Compliment Form: Invert each bit then add 1

One of the neat features of Two’s Compliment form is that the leftmost bit is a 1 for negative numbers and is a 0 for positive numbers. So, you or a computer can just look at that bit to determine the sign. Another neat thing is that if you want to convert -27 to +27, you just do the exact same process: invert each bit and add 1. So, this process really just inverts the sign of a number.

And, there’s more! If you want to do subtraction, you can just add the 2’s compliment of the number you want to subtract, and you get the right answer. For example, determine 12-5, which is the same as 12+(-5).

Example: 12-5

Convert -5 to 2’s Comp form:
– Write 5 in binary: 0101b
– Invert each bit: 1010b
– Add 1: 1011b – This is the 2’s Comp form of -5

Now, do binary 12 plus the 2’s Comp form of -5

`      1100      +1011    ---------     1 0111`

See, the last four digits contain the correct answer of 7. You do have to ignore that 1 that got carried over beyond the number of digits you are working with. Actually, no matter how many digits you are using, this operation will always result in a 1 being carried over beyond your number of digits. Now, let’s do an example that results in a negative number: 5-12, which is the same as 5+(-12). For this we actually have to use more than 4 digits. You always have to use at least one more digit than your numbers occupy in positive form, because you need that extra bit to represent the sign. We can use 8 bits for this example, although 5 is the minimum number needed.

Example: 5-12

Convert -12 to 2’s Comp form:
– Write 12 in binary: 00001100b
– Invert each bit: 11110011b
– Add 1: 11110100b – This is the 2’s Comp form of -5

Now, do binary 5 plus the 2’s Comp form of -12

`      00000101      +11110100      ---------       11111001`

The answer is 11111001b. Do you think that is -7 in 2s Comp form? Invert each bit: 00000110b, and add 1: 00000111b. That’s 7. BAM!

## Bits, Bytes, and Nibbles

Regarding the number of digits in binary numbers, there are several numbers of digits commonly used in computers, and they each have a name. I realize that I slipped into using the familiar term of bit to represent one digit in the preceeding discussion. Check out the table below.

 Name # of Digits # of Values Range Binary Example Hex Value Decimal Value Bit 1 21 = 2 Unsigned: From 0 to 1 1b 0x1 1 Nibble 4 24 = 16 Signed: From −8 to 7 Unsigned: From 0 to 15 0101b 0x5 5 Byte 8 28 = 256 Signed: From −128 to 127 Unsigned: From 0 to 255 00110101b 0x35 53 Word 16 216 = 65536 Signed: From −32,768 to 32,767 Unsigned: From 0 to 65,535 0100000010000000b 0x4080 16512 Double-Word 32 232 = 4294967296 Signed: From −2,147,483,648 to 2,147,483,647 Unsigned: From 0 to 4,294,967,295 01000000100000000100000010000001b 0x40804081 1082146945

The terms signed and unsigned in the table refer to whether you allow only positive numbers or whether you allow both positive and negative using two’s compliment form. In the signed case using two’s compliment form, the leftmost bit is the sign bit and the rest of the bits represent the number.