Vocademy

Math by Machine

Addition

The root of all mathematics is addition. Any mathematical operation can be performed by skilled use of addition. For example, multiplication is successive addition. Subtraction can be done by converting one number to a negative number, then adding the two numbers together. Division can be done by successive subtraction. The rules for binary addition are as follows:

Binary Addition

0
+0
0

0
+1
1
0 + 0 = 0   0 = 1 (or 1 + 0) = 1
  0 + 0  and 0 + 1 are the same as with decimal addition.
 

1  
1
+1
10
 
1
1
+1
11
1 + 1 = 0 and carry over to the next column to the left 1 + 1 + 1 (as with a carry from the column to the right) = 1 and a carry over to the next column to the left
 
In decimal addition 1 + 1 = 2. However, there is no 2 in binary numbers, so 1 + 1 = 10 (that's one-zero, not ten). Just as 9 + 1 = 0 and a carry, in binary 1 + 1 = 0 with a carry. If you have 1 + 1 and a carry from the previous column, the result is 1 with a carry.

Notice that the binary addition is identical to the Exclusive OR (XOR) function. A logical adder is simply an XOR gate with some extra gates to handle incoming and outgoing carries. The following is a diagram of an adding circuit made with two relays. This circuit can handle adding two bits along with an outgoing carry but doesn't handle an incoming carry. The circuit is called a Half-adder:

Relay Adder
A half-adder made with relays.

Points A and B are where the bits are input to the circuit (+5 volts = 1, and  0 volts = 0).

The tables show the current state of the circuit (boxed in red) and the other three possible states.

Point C is the sum of the inputs, and above that is the carry output.

You can trace the circuit through the switches of the relays. Where the circuit is complete, the output is +5 volts or a logical 1. Otherwise, the output is 0 volts or a logical 0.

This diagram shows the input as 0 and 0 and shows the sum as 0. There is no carry.

 

Relay Adder
In this diagram, the input is 0 and 1 (A is 0 and B is 1). The output at C is 1. There is no carry. This emulates binary addition of 1 + 0.

 

Relay Adder
In this diagram, the input is 1 and 0. Again, the output at C is 1 with is no carry. This emulates binary addition of 0 + 1.

 

Relay Adder
In this diagram, the input is 1 and 1. Now, the output at C is 0 and there is now a carry. This emulates binary addition of 1 + 1 (1 + 1 = 0 with carry).

Combining this with another half-adder to handle an incoming carry creates a full adder. For example, the following circuit consists of four full adders. It will add two 4-bit numbers, handling all carries. It can be cascaded with other full adder circuits to handle more bits.

Relay Adder

Subtraction

Complementing a binary number reverses all bits. For example, the complement of 1001 is 0110. This is also called the one's complement. The two's complement is created by performing a one's complement and then adding one. For example, add one to 0110 (the one's complement of 1001) and get 0111, the two's complement of 1001.

Subtraction is performed by calculating the two's complement of the subtrahend, adding that to the minuend, then removing any carry left over. Here is an example:

  Decimal   binary   two's
complement
Add   Reomve
Carry
Minuend -12    1100     +1100   +1100
Subrtahend -10   -1010   0110 +0110   +0110
Difference -02   0010     10010   +0010
                 
Adding binary numbers using the two's complement (with decimal equivalent).

Multiplication

Multiplication is successive addition. For example, to multiply 3 by 5 you add 3 to itself 5 times. That is 3 + 3 + 3 + 3 + 3 = 15. In this example, the number 3 is the multiplicand, and the number 5 is the multiplier. To multiply these numbers with a computer, you write a program that adds the multiplicand to itself the number of times specified by the multiplier. Since multiplication is commutative, you can swap the multiplicand and the multiplier.

Shifting all of the bits in a number to the left is a shortcut that multiplies that number by 2.

Division

Division is successive subtraction. For example, to divide 15 by 3, subtract 3 from 15 repeatedly until you reach 0, then count how many times you subtracted such as follows:

Process    Count 
15 3   = 12   1
12 - 3   = 09   2
0 - 3   = 06   3
0 - 3   = 03   4
0 - 3   = 00   5

Dividing 15 by 3 using successive subtraction. Three can be subtracted from 15 five times. Therefore, 15 ÷ 3 = 5

You can subtract 3 from 15 five times to reach 0, so 15 ÷ 3 = 5. To do this with a computer, you write a program that follows this procedure.

Shifting all of the bits in a number to the right divides that number by 2.

Square root

Here's an algorithm that calculates the square root of a number:

1.    Guess the answer by dividing the number by 2
2.    Divide the number by the guess 
3.   Add the result of this division to the guess  
4.   Divide the sum by 2 
5.    Make this the new guess and go to step 2  
 

This is repeated until the answer cannot be refined further. All you have to do is write a computer program that follows these steps, and you can calculate square roots. Notice that the only math involved is addition and division. We have already established how to do these operations with existing CPU functions.

Once you can add, subtract, multiply, divide, and find square roots, you can combine these operations to perform any mathematical operation. Algorithms to perform these operations are a branch of computer science beyond the scope of this course. However, these examples show how computers perform math.

Vocademy