Search code examples
javaexpression-evaluation

Java: arithmetic mechanics on a basic level or evaluation strategies


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?


Solution

  • 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 :)