0

I was looking at bitwise ways to multiply and add numbers without using * or + operators. But I want to know that it might be possible that java may already be using a better approach underneath. I tried to find the algorithm for *,+,_,and / operations. I read some paragraphs of JVM Specification but could not find satisfactory answers.

for example:

int a = 33;
int b = 44;
int c = a + b;

Now what I want to know is when java encounters + operator where does it finds the implementation for adding those numbers. Same goes for multiplication, division and subtraction. Guide me to find the answer.

3
  • 2
    It'll simply use the corresponding instructions of your CPU (For 64 bit integers on 32 bit CPUs it needs to handle the two halves of the integer separately). Commented Jul 22, 2016 at 8:56
  • You won't find the answer in the specification, because it only cares about the result and not about how it's implemented. Commented Jul 22, 2016 at 8:59
  • I believe the thing that does this is called the arithmetic logic unit. But as far as Java is concerned it could be a monkey with an abacus. Commented Jul 22, 2016 at 11:30

1 Answer 1

6

When Java encounters this + operator, it compiles the addition to byte code, specifically an iadd instruction.

What the computer does when executing this instruction depends on how the virtual machine on which this program is executed was programmed. It might delegate to an ADD opcode from some processor's instruction set. It might consult an internal lookup table for small values. It might do something completely else that you would never in a million years have thought of.

The point of writing for a virtual machine is that it guarantees the semantics of each operation but not its implementation, so by definition, you can't really know what happens.

6
  • Some articles on the internet say that using bitwise hacks for arithmetic operations are faster than the native implementation of the language. So does using bitwise operators faster than using +,-,*,/. In java or other programming languages.
    – Anant666
    Commented Jul 22, 2016 at 9:04
  • 1
    Well, not in Java, since as I explained, you cannot ensure what happens at the processor level. A common optimization in other languages is to replace multiplication/division by powers of two by bit-wise shifts, but every half-way intelligent compiler already does this nowadays. Commented Jul 22, 2016 at 9:08
  • @Anant666 - xkcd.com/386 applies here. I've never found a language where bitwise hacks are faster than the standard arithmetic operators for ordinary integers, and such a language would need to be so badly implemented that nobody sane would use it.
    – Jules
    Commented Jul 22, 2016 at 10:17
  • @KilianFoth - the compiler doesn't even really need to do that any more most of the time, anyway: while multiply instructions may technically be slower than add instructions on most processors, the situations where you'll notice the difference are rather rare, therefore naive benchmarks (e.g. this one) tend to show no difference, or even sometimes addition being slower. Most of the time, your memory bandwidth will be the limiting factor, not calculation speed.
    – Jules
    Commented Jul 22, 2016 at 10:25
  • 1
    @Anant666: Many articles on the internet are written by people who don't have a clue. If you use the + operator for example, the compiler will use the fastest way to add numbers according to the language spec. Well, not always: Sometimes it will figure out that it can get the required result without actually adding numbers and do that.
    – gnasher729
    Commented Jul 22, 2016 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.