Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do computers add,subtract,etc?

  1. Apr 9, 2004 #1
    How do computers do math? I mean, it seems kinda like the math part is the most basic of the functions of the computer.

    But I mean, how does it KNOW that 1+1=2?

    To give a background, I understand binary: 1+1=10. I have a rudimentary understanding of assembly, I tried to program in it many years ago, so I could look at this:

    mov ax, 1
    add ax, 1

    But how does that add 1 into ax, and then have the value of 2? (or b10).

    I can almost see how like shl, and shr (is that square and square root?) can work, but shifting the bits, but not adding, etc.

    I'm assuming the microchip (x86?) has to be preprogrammed to know the order of numbers.. But I have no clue. May someone shed light on this?
  2. jcsd
  3. Apr 9, 2004 #2
    How do you add decimal numbers?
    Code (Text):
    + 23
    So why can't you do that in binary as well?
    Code (Text):
    +  11
    Do you refer to 1, 2, 3, 4, ... as the "order of numbers"? If so, no, a computer doesn't the inifinite series of numbers programmed in it.
  4. Apr 10, 2004 #3
    Well, I know that I can add numbers, because I can visualize it. I can see that if I have three objects, and take in an additional 4 objects, that I can now count 7 objects. But how does a computer do that?

    Even this equation: 4+1=5. If you just put the value "4" in a computer, and you add the value of 1 to it, how does it know how to add?
  5. Apr 10, 2004 #4
    How do you add 1 to 4? Probably "intuitively", because these are simple numbers. But what about 412793 + 23871?

    If you tell a computer to add 1 to 4, it first takes both numbers and converts them to binary. 1 becomes 1, 4 becomes 100. Now it adds them 1 + 100 = 101 and converts the answer back to decimal: 5.
  6. Apr 10, 2004 #5
    I think I am doing a poor job at conveying what I am trying to ask.

    How does the computer know how to add 1 + 100 to give 101 ? I mean, how do you program a computer to be able to add numbers?
  7. Apr 10, 2004 #6
    Let's start with 4 empty registers, and call them A, B, C and R. We want to add A to B, use the C register for carrying results and R for storing the result. (Obviously this is done a lot more efficiently inside the computer, but I'm trying to simplify things.)

    C = 0000
    A = 1011
    B = 0011
    R = 0000

    The computer goes from right to left, and does the following: it adds* the two bits in A and B, saves the result in the R register. After the first operation the registers look like this:

    C = 0010
    R = 0000

    This is because 1 + 1 = 10, so we put 0 in the result and save 1 in the carry. Now the computer continues for the 2nd pair of bits, again 1 and 1. 1 + 1 = 10, but we also have a carry from earlier! So it adds the carry to the result 10 + 1 = 11 and registers it:

    C = 0110
    R = 0010

    And it does the same again and again until it's done.

    * You may still not understand how computer can add 1 and 1, or 1 and 0, or 0 and 0. This is simple and can be done with two gates, AND and XOR. Consider the following truth table:
    Code (Text):
     A | B | AND | XOR
     0 | 0 |  0  |  0
     0 | 1 |  0  |  1
     1 | 0 |  0  |  1
     1 | 1 |  1  |  0
    If (A XOR B) is 1, the result is 1. Otherwise, the result is 0. As for the carry, that is determined with by the (A AND B) result - if it's 1, there is carry, otherwise there isn't.

    Again I should stress that the computer does this much more efficiently. For example, it doesn't use a whole register for the carrying - it uses one bit in a special flag register to know whether or not the last operation had a carry. Also, it doesn't store the result in a completely new register, it stores it in the one of the registers A or B (as you probably guessed by the look of the ADD command in Assembly).
  8. Apr 10, 2004 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Chen is showing you the mechanics, the reason the computer knows how to do those mechanics is because at some point in time a programmer wrote a set of instructions and assigned a number to that set of instructions. When you give the compiler a ADD command it transforms that mnemonic into the number associated with the set of instructions for adding 2 numbers (or registers). When the CPU encounters the number associated with a set of instruction it blindly executes that command. This is the work of low level programmers to create the set of instructions that the CPU actually executes. I believe one of the least favorite courses toward a CS degree is the course in compilers where you will write the code for a simple compiler.
  9. Apr 10, 2004 #8
    Ahhhhhhhhh, I honestly think I understand it now! It was the thing about the (logic, aren't they called??) gates.

    Since this is how computers do the most basic stuff, with binary and the gates and all that, would it be kinda impossible to have a different base langauge for computers? I mean, say you used hex as the most basic thing, could you do logic gates with hexadecimal?

    By the way, thank you both for writing that stuff out. That hit it home. I really appreciate it.
  10. Apr 10, 2004 #9
    Yes, they are called logic gates. What kind of meaning would (4 AND 7) have, though? I don't know if it would be impossible, but certainly very very difficult and not worthwhile (what would be the advantages?).
  11. Apr 10, 2004 #10


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    All logic is done in binary, the reason we use hex is the ease of translation between hex and binary. 1 hex digit translats to 4 binary digits. Not only is the Hex is easier to "read" then binary, it is far more compact. So the Hex is for humans, but it make the binary very accesable.

    0001 = 1

    so 4 and 7 = 0010 and 0111 = 0010
    Last edited: Apr 10, 2004
  12. Apr 11, 2004 #11
    Let's see you plug that into a single gate operation, though. :smile: (That is what I meant, obviously there is a meaning to 4 AND 7.)
  13. Apr 11, 2004 #12


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member


    You are correct each individual gate handles a single bit at a time. Thus the old 8 bit computers (where I did a bit of machine language dabbling) had sets of 8 logic gates, new ones,... we're up to 64 now, I believe.
  14. Apr 11, 2004 #13
    And these gates, in actuality are they just some type of thing that controls electricity?
  15. Apr 11, 2004 #14


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member


    Just a bunch of really small transistors, for the most part.
  16. Apr 11, 2004 #15


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Here is a thread which dicsusses operation of a basic solid state device.
  17. Apr 11, 2004 #16


    User Avatar
    Staff Emeritus
    Gold Member

    Basically, all of this comes from building appropriate gates. The same way you can make an "and" gate, that produces the proper "output" given two inputs, you can put together one that "adds" and gives you also a carry-on bit, which you can then add to the next two binary digits.

    The design of these type of circuits is actually one level below compilers. IIRC, it is called "microprogramming", and it can be done using logic gates and "flip-flop" circuits, which allow you to synchronize the operation of a circuit with a "clock" signal, so that operations are performed in the proper sequence.
  18. Apr 12, 2004 #17


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member


    Not quite. Microprogamming refers to CISC (Complex Instruction Set Computing) processors, in which large instructions are internally broken down into a routine of more fundamental instructions. Microprogramming has gone the way of the dinosaur, thankfully.

    The simple answer is that a computer processor is made of tiny transistors, which can switch electrical current. The transistors are combined together into groups with function as logic gates, and can perform simple operations like AND, OR, NOT, XOR, and so on. These logic gates can then be grouped into larger-scale strucures which can take in two 8-bit (or 16-, or 32-, or 64-) strings of binary digits and produce their sum or difference. Some intelligent person who designed the processor wired up his logic gates in the proper way to perform those operations.

    - Warren
  19. Jun 30, 2004 #18
    Hey I know this an old thread, but tell me if it is just chance, or does it actually mean something, the above. I'm not sure what I mean, but check it out.

    The operation: (4 and 7), into binary gives 0100 and 0111 = 0100. So it gives 4. That's kinda like in statistics or whatever when you have two sets and you do the "and" thing, you get whatever is in common with the two sets. And the first four digits of the numbers that comprise 7 are also in the number 4. That's neat, right?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook