Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do computers do arithmetic?

Posted on 2004-10-28
13
Medium Priority
?
1,116 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:jetforce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +4
13 Comments
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 500 total points
ID: 12431612
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12431779
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
 
LVL 1

Expert Comment

by:Bharat23
ID: 12431822
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:rhyuso
ID: 12432154
Most microprocessors actually use 2's complement to do subtraction. This is a 1's complement + 1.

0
 
LVL 1

Expert Comment

by:rhyuso
ID: 12432210
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
 
LVL 3

Expert Comment

by:georg74
ID: 12432417
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
 
LVL 3

Expert Comment

by:georg74
ID: 12432440
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
 
LVL 16

Expert Comment

by:imladris
ID: 12434441
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
 
LVL 3

Expert Comment

by:georg74
ID: 12435862
> 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;
ADD operation requires about 16 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
 
LVL 3

Expert Comment

by:georg74
ID: 12436123
jetforce,

you asked about "how computers do arithmetic",
but you accepted an answer providing information about binary arithmetics
(which is just the theoretical base for how computers do arithmetics).

can you please explain your decision?
g.
0
 
LVL 3

Author Comment

by:jetforce
ID: 12442611
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
 
LVL 3

Expert Comment

by:georg74
ID: 12443752
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
 
LVL 1

Expert Comment

by:Bharat23
ID: 12450578
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question