-2

Excuse me for the vagueness of the question, but I'm a relatively new programmer.

How exactly does the computer know how to do arithmetic? Does every language - specifically Java, have an add, subtract, addition, subtraction, modular division class?

If so, how does the class scan for the operator signs?

Pavel
  • 1,519
  • 21
  • 29
Andrew Lin
  • 13
  • 1
  • Are you asking about the underlying computing structure, or how does the compiler turn written code into an executable format (or bytecode in this case)? – phflack Oct 27 '17 at 13:36
  • 1
    Have a read of this - http://www.informit.com/articles/article.aspx?p=31670&seqNum=2 – achAmháin Oct 27 '17 at 13:39
  • Ideally, if you really strong enough to do long reading: https://mitpress.mit.edu/sicp/full-text/book/book.html , ideally start your learning from other programming languages like ML, Haskel etc – Pavel Oct 27 '17 at 13:42
  • @notyou add that as a answer. – Sridhar Oct 27 '17 at 13:43

1 Answers1

0

Computers actually only have addition and can't subtract, multiply, or divide. It performs the addition at the bit level. Binary arithmetic is the only means by which any electronic digital computing machine can perform arithmetic.

Because the computer can only add, it cannot do the subtraction you ask. However, it can take the negative of a number and represent (at the bit level) the negative value, i.e. 42+(-6)+(-6)

For multiplication and dividing, it can only simulate it, i.e. To divide 42 by 7, the computer subtracts 7 from 42 (well, it adds the negative of 7 to 42) until it reaches zero and counts the number of times (6) it took to reach zero.

This is just a small summary of this, so feel free to read more :)

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • I'm not sure I understand - you say that in modern programming languages multiplication is implemented by addition of argument specified number of times? If so then you are strongly mistaken. – Shadov Oct 27 '17 at 14:05
  • Good answer, you satisfied my curiosity on the coding level, but on the logical level, with only on/off states, how does binary addition work? How do you create addition from 1's and 0's. I am assuming you build upon logic gates? – Andrew Lin Oct 27 '17 at 14:23
  • @AndrewLin [First google result explains it pretty well](http://web.math.princeton.edu/math_alive/1/Lab1/BinAdd.html), and so does the [wikipdia page](https://en.wikipedia.org/wiki/Binary_number#Addition) – phflack Oct 27 '17 at 16:57