Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The div codes

By now, this process should seem familiar. You can probably guess that there are four division instructions, that they are idiv, ldiv, fdiv, and ddiv, and that they divide ints, longs, floats, and doubles, respectively.

Division, like subtraction, is not commutative. Ten divided by five is two, but five divided by ten is one half. (In Java, 5 / 10 is actually 0, because integer division truncates toward zero.) The first value popped from the stack is divided into the second number popped from the stack. In other words, the first number popped from the stack is the dividend, and the second number popped from the stack is the divisor. Figures 5-26 and 5-27 demonstrate this.


Figure 5-26  5 / 10 = 0.


Figure 5-27  10 / 5 = 2.

Floats are divided exactly the same way, except that no truncation towards zero is necessary. Longs and doubles are also, except that each number requires two words. Figure 5-28 shows what happens when you divide 10L by 5L.


Figure 5-28  10L / 5L = 2L.

The rem codes

The rem codes are used for the Java remainder operator, %. As usual, there are four of them — irem, lrem, frem, and drem — one each for ints, longs, floats, and doubles. Like division, taking the remainder is not commutative. 10 % 2 is 0, but 2 % 10 is 2. The first value popped from the stack is divided into the second number popped from the stack, and the remainder is pushed onto the stack. Figures 5-29 and 5-30 demonstrate.


Figure 5-29  2 % 10= 0.


Figure 5-30  10 % 2 = 5.

Floats are handled exactly the same way. Longs and doubles are too, except that each number requires two words. Figure 5-31 shows what happens when you take the remainder of 10L by 2L.


Figure 5-31  10L % 5L = 0L.

The neg codes

The final group of arithmetic instructions is different. These are the neg instructions — ineg, lneg, fneg, and dneg — for ints, longs, floats, and doubles, respectively. Each of these pops only one value from the stack, not two. The instructions ineg and fneg pop one 32-bit word. The instructions lneg and dneg pop two 32-bit words. In either case, the value is negated; that is, its sign is changed, and the result is pushed back onto the stack. This is semantically equivalent to multiplying by -1, although in general, using the negation operator will be faster. The size of the stack does not change. Figure 5-32 shows the negation of the int value 255. Figure 5-33 shows the negation of the long value -32.


Figure 5-32  Negating 255 (0x000000FF) to get -255 (0xFFFFFF01).


Figure 5-33  Negating -32L (0xFFFFFFFFFFFFFFE0) to get 32L (0x0000000000000020).

Bit manipulation

The bit-level byte code instructions map very closely to the bit-level operators. For each bit-level operator like << or ~, there are exactly two instructions, one for ints and one for longs. (Remember that you can’t use bit-level operators on floats, doubles, or references.)

Shift operators

There are six shift operators: ishl, lshl, ishr, lshr, iushr, and lushr. Each pops two operands from the stack and returns the result to the stack. The instructions beginning with i operate on ints, and the instructions beginning with l operate on longs.

Shift left

The ishl and lshl instructions correspond to the << operator. They’re also used by the <<= operator. The first value popped from the stack is the number of bits to shift left. The second value popped from the stack is the value that will be shifted. The vacated bits are filled with zeroes. When an ishl instruction has completed, the stack is one word shorter. When an lshl instruction has completed, the stack is two words shorter. Figure 5-34 shows the int value 255 being left-shifted eight places. Figure 5-35 shows the long value -32L being left-shifted eight places.


Figure 5-34  255 << 8.


Figure 5-35  -32L << 8.

Shift right

The ishr and lshr instructions correspond to the >> and >>= operators. The first value popped from the stack is the number of bits to shift right. The second value popped from the stack is the value that will be shifted. The vacated bits are filled with the sign bit — 0 for positive numbers and 0 or 1 for negative numbers. When an ishr instruction has completed, the stack is one word shorter. When an lshr instruction has completed, the stack is two words shorter. Figure 5-36 shows the int value 255 being right-shifted four places. Figure 5-37 shows the long value -32L being right-shifted eight places.


Figure 5-36  255 >> 4.


Figure 5-37  -32L >>> 8.

Unsigned shift right

The iushr and lushr instructions correspond to the >> and >>= operators. The first value popped from the stack is the number of bits to shift right. The second value popped from the stack is the value that will be shifted. The vacated bits are filled with zeroes, regardless of the sign of the number. When an iushr instruction has completed, the stack is one word shorter. When a lushr instruction has completed, the stack is two words shorter. Figure 5-38 shows the int value -32 being unsigned-right-shifted eight places.


Figure 5-38  -32 >> 8.

Combination

The bitwise operators &, |, and ^ each have two corresponding byte code instructions, one each for ints and longs. These are iand, land, ior, lor, ixor, and lxor. Each behaves exactly as you would expect. The int instructions pop two words off the operand stack, combine them with the appropriate operator, and push the result back onto the stack. The long instructions pop four words off the operand stack, combine them with the appropriate operator, and push two words back onto the stack. Figure 5-39 shows the conjunction of the ints -32 and 8. Figure 5-40 shows the disjunction of the ints -32 and 8. Figure 5-41 shows the xor of the longs -32 and 255.


Figure 5-39  -32 & 8.


Figure 5-40  -32 | 8.


Figure 5-41  -32L ^ 255L.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.