3.3. Arithmetic

The Java Virtual Machine generally does arithmetic on its operand stack. (The exception is the iinc instruction, which directly increments the value of a local variable.) For instance, the align2grain method aligns an int value to a given power of 2:

int align2grain(int i, int grain) {     return ((i + grain-1) & ~(grain-1)); }

Operands for arithmetic operations are popped from the operand stack, and the results of operations are pushed back onto the operand stack. Results of arithmetic subcomputations can thus be made available as operands of their nesting computation. For instance, the calculation of ~(grain-1) is handled by these instructions:

5   iload_2        // Push grain 6   iconst_1       // Push int constant 1 7   

Get The Java® Virtual Machine Specification, Java SE 7 Edition, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.