Solved

How do computers do arithmetic?

Posted on 2004-10-28
1,105 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
Question by:jetforce
    13 Comments
     
    LVL 11

    Accepted Solution

    by:
    0
     
    LVL 9

    Expert Comment

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

    Expert Comment

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

    0
     
    LVL 1

    Expert Comment

    by:rhyuso
    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
    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
    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
    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
    > 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
    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
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Suggested Solutions

    Title # Comments Views Activity
    Adobe Customization Wizard XI issues 26 109
    matchUp  challenge 6 29
    sameEnds challenge 25 47
    canBalance challenge 34 42
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now