Numerical operations fall into two categories: Arithmetic operations: addition, subtraction, multiplication, quotient, and remainder Bitwise operations: shifts and boolean operations (OR, XOR, NOT, AND, etc.)
## 3.8.1 Arithmetic OperationsArithmetic operations are the four operations you learned in elementary school: addition, subtraction, multiplication, and division. Actually, division is really two different operations: quotient and remainder. There is also an additional operation, negation, which is really just multiplication by 1. This is provided because most computers have hardware that can perform negation more quickly than going through the steps of multiplication. Each of these six operations is defined on each of the four basic numerical types: Each of these instructions takes no arguments. The operands are found on the stack. All of them except the negation take two operands; negation takes only one. The operands are popped off the stack and the result is pushed on in their place. When order matters, as in subtraction, the top of the operand stack is subtracted from the next element; division is similar. For example, to calculate (100000 99*(22222/3+7) ): ; Stack: ldc 1000000 ; 100000 bipush 99 ; 100000 99 sipush 22222 ; 100000 99 22222 iconst_3 ; 100000 99 22222 3 idiv ; 100000 99 7407 bipush 7 ; 100000 99 7407 7 iadd ; 100000 99 7414 imul ; 100000 733986 isub ; 633986 ineg ; 633986 Notice that the type of the result is always the same as the type of the operands. If you want to subtract a
## 3.8.2 Nonnumbers and InfinityThe JVM fconst_0 fconst_0 fdiv ; Yields NaN ldc 3.14159 ; Push Pi fadd ; Yields NaN ldc 2.71828 ; Push e fconst_0 ; Push zero fdiv ; Yields infinity ldc 42.0 ; Push some other number fadd ; infinity + anything = infinity dup ; Put two infinities on the stack sub ; infinity infinity = NaN The standard Java libraries keep ## 3.8.3 Bitwise OperationsYou can think of
Bitwise operations are used for performing boolean operations on the bits of The bits of a number are like the digits of your odometer. If 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110 That's 32 ones for 1, and 31 ones followed by a zero for 2. If the leftmost bit is 1, then the number is interpreted as negative. (Numbers are grouped into sets of four for easier reading.) Suppose you want to shift this number to the right one step. What do you put in the leftmost place? Should it be 0, 1, or something else? The JVM answers this question two different ways. The standard right shift, 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 That's 1 in both cases. The standard right shift by 1 is just like division by 2, rounding down. To shift by 2 is like dividing by 4; shifting by 3 is like dividing by 8. You get the idea. The unsigned shift, 0111 1111 1111 1111 1111 1111 1111 1111 or 2,147,483,647. This is the largest positive number you can get in the JVM with A common use for Here's an example of some bit twiddling on ; Stack iconst_m1 ; 0xFFFFFFFF ldc 0x12345678 ; 0xFFFFFFFF 0x12345678 ldc 0x87654321 ; 0xFFFFFFFF 0x12345678 0x87654321 iand ; 0xFFFFFFFF 0x02244220 ldc 0xFFFFCCCCC ; 0xFFFFFFFF 0x02244220 0xFFFFCCCC ior ; 0XFFFFFFFF 0xFFFFCEEC ixor ; 0x00003113 bipush 16 ; 0x00003113 16 ishl ; 0x31130000 iconst_2 ; 0x31130000 2 ishl ; 0xC44C0000 iconst_2 ; 0xC44C0000 4 ishr ; 0xF1130000
## 3.8.4 Floating-Point Arithmetic and |

Programming for the Javaв„ў Virtual Machine

ISBN: 0201309726

EAN: 2147483647

EAN: 2147483647

Year: 1998

Pages: 158

Pages: 158

Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net