# How do computers do arithmetic?

A long time ago when I was at college,
I vaguely remember that computers do all there multiplications and division by adding and subtraction, or it was the other way round. I have searched the net and cannot found anything about it.
Could anybody explain how it is done, preferrable with some examples.

Thanks

Jetforce
LVL 3
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Do some reading on boolean logic:

http://computer.howstuffworks.com/boolean.htm

And Electric gates:

http://electronics.howstuffworks.com/digital-electronics.htm

Good luck,
[r.D]
0
Commented:
Hi,
All computers do their arithmetic through addition. For subtraction, 1's complement is performed and so on. To know in detail about this you can read any book on logic design. Your question is a subject by itself in Computer Science Engineering.
:)
0
Commented:
Most microprocessors actually use 2's complement to do subtraction. This is a 1's complement + 1.

0
Commented:
If you want to know how to do the multiplication try this site

http://www.andraka.com/multipli.htm

The sites above seem to be about the math involved rather than the actual way in which the processors ALU works.
0
Commented:
Hi,

As you pointed, computers do arithemtics by using basic binary operations provided by available electronic circuits.
Here is a simple overview over those layers of basic operations, the way _computers_ do it. An operation in the next levels is accomplished by combining operations of the previous layers.

1 - binary AND, binary NOT - these two operations are enough to do all of the arithmetics
so the really basic operation is not addition etc, but simplest logic
x  NOT x
-  -------
0    1
1    0

x y  x AND y
---  ---------
0 0   0
0 1   0
1 0   0
1 1   0

2 - binary addition, binary multiplication 2 ("shifting bits left") or division by 2 ("shifting bits right")
therefore you can say "it's adding and multiplying by 2"

binary addition:   0101 + 0011 = 1000   (5 + 3 = 8)
multiplication by 2:  0101 << 1 = 1010  (5 * 2 = 10) (read: shift left by 1)
division by 2: 1000 >> 1 = 0100 (8 / 2 = 4) (read: shift right by 1)

3 - further unsigned (greater or equal 0) integer operations
* subtraction is addition of the 2's complement:

x - y = (x + (C - y)) - C with C = 2^number of bits
this "C - y" is the 2's complement of y and can be calculated by inverting all bits (NOT operation) and adding 1:
to continue 4-bit examples: C = 2^4 = 16, so x - y = x + ("16" - y) + "16" when using 4 bits
0101 (5) gives 1011 ("-5"), so it is "-5" = 11
0011 (3) gives 1101 ("-3"), so it is "-3" = 13

8 - 3 = 5 is calculated as (8 + ("-3")) - 16 = 8 + 13 - 16 = 5
this - 16 happens automatically by non-existence of a 5th bit
1000 - 0011 = 1000 + 1101 = 0101

* multiplication is a combination of multiplication by 2 and addition:

x * 10 is calculated as (x * 2 * 2 + x) * 2 = (x * 4 + x) * 2 = x * 5 * 5 = x * 10

* integer division is carried out in a similar way

x / 10 = (x / 2) / 5 = ((x / 2) - 5) / 2 / 2
x / 3 = (x - 3) / 2
example: 8 / 3 = 2
1000 / 0011 = (1000 - 0011) >> 1 = 0101 >> 1 = 0010

4 - signed integer (negative and non-negative) are representes as 2's complement, and the operations reduce to the operations above. special handling of "sign", "overflow" and "carry" is added.

example. -5 + 8 = 3
1011 + 1000 = 0011

5 - higer level integer operations like power or logarithm require a bit more complicated calculation - they are carried out using operations from previous levels.

x^2 = x * x
x^3 = x^2 * x
x^4 = x^2 * x^2
etc.

6 - "floating point" operations are reduced to integer operations where the position of decimal point is calculated parallel to basic operation:

3.5 + 2.5 = 6 can be represented as 011.1 + 010.1 = 110.0
that is (7/2) + (5/2) = (12/2), we calculate 3.5 + 2.5 the same way as 7 + 5

7 - transcedental functions as sine, cosine etc. are (partially) estimated using built-in tables, because that is much faster than algorithmic computation

example: for sinus function, only 1/4 of the full period (that is 0 to PI/2) needs to be present in the lookup table, because:
sin(x) = sin(PI/2 - x)
example: for cosinus, sinus is used, because cos(x) = sin(x + PI/2)
etc.

8 - you'r software starts here, it's up to you how you will do it :-)

cheers,
georg
0
Commented:
correction (LOL)
1 AND 1 = 1

x y  x AND y
---  ---------
0 0   0
0 1   0
1 0   0
1 1   1

g.
0
Commented:
Actually, the basic operation for addition is an exclusive or:

x y  x XOR y
---  ---------
0 0   0
0 1   1
1 0   1
1 1   0 (and generate a carry)
0
Commented:
> Actually, the basic operation for addition is an exclusive or

in the terms of the algebra, yes.

but when talking about real circuits (and real computers),
THE real basic operation is NAND(x, y) = NOT(x AND y).
this is because it requires just a single transistor and it is
enough to build every other operation, i.e. the whole logic.

x y  x NAND y
- -  -----------
0 0   1
0 1   1
1 0   1
1 1   0

actually, the XOR operation is composed out of four NAND operations, for example in following way:

a = x NAND y (gate 1)
b = a NAND x (gate 2)
c = a NAND y (gate 3)
d = b NAND c = x XOR y (gate 4)

i.e. x XOR y = NOT{NOT[NOT(x AND y) AND x] AND NOT[NOT(x AND y) AND y]}

(with the given technique, it is not possible to produce XOR operation with less than 4 transistors;

cheers,
georg

PS: one more correction to the above:
x * 10 is calculated as (x * 2 * 2 + x) * 2 = (x * 4 + x) * 2 = x * 5 * 2 = x * 10
0
Commented:
jetforce,

(which is just the theoretical base for how computers do arithmetics).

g.
0
Author Commented:
I really do not want to get in an argument about this, as a good will gesture, I will post a question with 100 pts just for you. If this is satisfactory, just reply here.
0
Commented:
It would be against EE rules. Anyway, thanks for the offer.
Just try to avoid such situations. People invest some effort in answering...
you get the point.
g.
0
Commented:
Agree with georg74 on this.. not nitpickin over a few points.. But, a 'fair deal' is what keeps this site so great to be on.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.