Logic and Integer Arithmetic
Java's Bytecodes for Logical and Arithmetic Operations
by Bill Venners
First Published in JavaWorld, November 1996

<<  Page 3 of 3

Logical results: A JVM simulation
The applet below demonstrates a Java virtual machine executing a sequence of bytecodes. The bytecode sequence in the simulation was generated by javac for the incrementLogically() method of the class shown below:

```class VulcanCounter {     void incrementLogically() {         int spock = 0;         while (true) {             int tempSpock = spock;             for (int i = 0; i < 32; ++i) {                 int mask = 0x1 << i;                 if ((tempSpock & mask) == 0) {                     tempSpock |= mask;                     break;                 }                 else {                     tempSpock &= ~mask;                 }             }             spock = tempSpock;         }     } }```

The actual bytecodes generated by javac for incrementLogically()
are shown below:

```  0 iconst_0 // Push int constant 0.  1 istore_0 // Pop to local variable 0: int spock = 0;  2 iconst_0 // Push int constant 0.  3 istore_1 // Pop to local variable 1: int i = 0;  4 goto 33 // Jump unconditionally ()  7 iconst_1 // Push int constant 1.  8 iload_1 // Push local variable 1 (i).  9 ishl // Arithmetic shift left top int (i) by next to top int (1). 10 istore_2 // Pop to local variable 2: int mask = i << 0x1; 11 iload_0 // Push local variable 0 (spock). 12 iload_2 // Push local variable 2 (mask). 13 iand // Bitwise AND top two ints: (spock & mask) 14 ifne 24 // Jump if top of stack is not equal to zero: if ((spock & mask) == 0) { 17 iload_0 // Push local variable 0 (spock). 18 iload_2 // Push local variable 2 (mask). 19 ior // Bitwise OR top two ints (spock | mask) 20 istore_0 // Pop to local variable 0: spock |= mask; 21 goto 2 // Jump unconditionally (to top of while): break; 24 iload_0 // Push local variable 0 (spock). 25 iload_2 // Push local variable 2 (mask). 26 iconst_m1 // Push -1. 27 ixor // Bitwise EXCLUSIVE-OR top two ints: ~mask 28 iand // Bitwise AND top two ints: spock & (~mask) 29 istore_0 // Pop to local variable 2: spock &= ~mask; 30 iinc 1 1 // Increment local variable 1 by 1: ++i 33 iload_1 // Push local variable 1 (i). 34 bipush 32 // Push integer constant 32. 36 if_icmplt 7 // Jump (to top of for) if next to top integer is less than top                      // integer: i < 32 39 goto 2 // Jump unconditionally (to top of while). ```

The incrementItLogically() method repeatedly increments an int without using the + or ++ operators. Only logical operators &, |, and ~ are used. An increment is accomplished by searching through the bits of the current int, starting with the lowest order bit, and turning ones to zeros. As soon as a one is encountered in the current int, it is changed to zero and the search stops. The resultant int now represents the old int incremented by one. The process is started again on the new int. Each incremented number is stored in the spock variable. Spock is local variable zero in the compiled bytecodes, so you can watch local variable zero count 0, 1, 2, 3, and so on.

To drive the simulation, just press the Step button. Each press of the Step button will cause the JVM to execute one bytecode instruction. To start the simulation over, press the "Reset" button. To cause the JVM to repeatedly execute bytecodes with no further coaxing on your part, press the "Run" button. The JVM will then execute the bytecodes until the Stop button is pressed. The text area at the bottom of the applet describes the next instruction to be executed. Happy clicking.

To view the Logical Results applet, visit the interactive illustrations of Inside the Java Virtual Machine at:

http://www.artima.com/insidejvm/applets/LogicalResults.html

Bill Venners has been writing software professionally for 12 years. Based in Silicon Valley, he provides software consulting and training services under the name Artima Software Company. Over the years he has developed software for the consumer electronics, education, semiconductor, and life insurance industries. He has programmed in many languages on many platforms: assembly language on various microprocessors, C on Unix, C++ on Windows, Java on the Web. He is author of the book: Inside the Java Virtual Machine, published by McGraw-Hill. Reach Bill at bv@artima.com.

This article was first published under the name Under the Hood: Logic and integer arithmetic in JavaWorld, a division of Web Publishing, Inc., November 1996.

<<  Page 3 of 3

 Web Artima.com