Chapter 12 integer arithmetic

12.1 twos complement arithmetic
Java virtual machine supports all integer types -byte, short, int, and long, they are with the two's complement code symbols. Twos complement described embodiment can be either a positive integer, it is possible to describe negative integer. In a two's complement number, the most significant bit is its sign bit. Sign bit is 1, represents a negative integer; sign bit is 0, a positive integer and represents the number 0.

The range of the number can be represented in twos complement scheme: The total number of times the power of 2 bits. For example, in Java, short type is twos complement signed 16-bit integer. Only integer can be represented by: 216 or 65536. type short half value range of 0 and is used to represent a positive integer, and the other half is used to represent a negative number. Range 16-bit twos complement negative is -32768 (0x8000) ~ -1 (Oxffff ), petty 0x0000 to represent an integer in the range of 1 (0x0001) -32 767 (0x7fff ).
Intuitively positive integer is just one of a number of two representations. Negative and negative may be derived by adding a 2 - power. For example, short type of 16-bit length, so twos complement representation can be obtained (-32768--1) within a valid range and by adding a negative power of 16 (or 65536) 2 of the negative. -1 two's-complement representation is 65536 + (-1) or 65 535 (Oxffff). -2 two's-complement representation is 65536 + (-2) or 65 534 (Oxfffe).

Addition operation performed on the complement signed binary number of symbols and adding performed on as an unsigned binary number. Two numbers together (ignoring overflow), the result is interpreted as a signed two's complement number. This process will be carried out in the calculation result is within the valid range of this type of situation. For example, to obtain the results 4+ (-2), and as long as the Oxfffffffe addition to 0x00000004. As a result Ox100000002, but because only 32-bit type int, then overflow portion is omitted, the result is 0x00000002.

Java virtual machine appear in integer arithmetic overflow does not result in throwing an exception, only the result is simply truncated to match the data type (or an int, or to type long). For example, the int type value 0x7fffffff and adding 1, will be 0x80000000. Therefore, if the added value of type int rather long, Java Virtual Machine 2147483647 would be a result of adding -2147483648. When programming in Java, it must always pay attention to overflow may occur. Must confirm the selected data in each case type (int or long) is correct. Integers are ArithmeticException will throw an exception when a division by zero, so you should always bear in mind such an exception will be thrown, must catch exceptions when necessary.

If the long size still can not meet the needs, may be used Biglnteger java.math package class, instances of this class can be described an integer of any length. Biglnteger class supports all mathematical operations performed on any length integer, provided that these operations are based on the basic type and the java virtual machine that is supported java.lang.Math packet
.

12.3 arithmetic operation code
Java virtual machine provides several operating code integer arithmetic operations, and they perform long int types of operations based. As described above, when byte, short, and char type arithmetic values participate, they will first converted to type int. For each type int performing arithmetic operation code, a corresponding long operation code in the same type of operation.

Integer addition and may be performed on long int types. Table 12-1 describes the operation code of the following tasks: pop the top two values ​​of the stack, adding the result onto find. Pressure must have a value added two people first instruction stack. The value of the type specified by the operation codes themselves, the final result is always the same type as the members added. These opcodes will not lead to any exception is thrown, where overflow is often ignored.

Earlier we get such a rule: operation opcode taken their operands from the stack, but showing in Table 12-2 - of exceptions, i.e. iinc operation code adding operation on the local variables of type int. Adding local variables for the stream of byte code in the first byte immediately following the instruction iinc, the value to be added after the local variables from the second byte of the instruction iinc removed. The second byte is interpreted as a signed two's complement 8-bit code. Local variables and 8-bit signed value is added, the addition result is written back to local variables. This code can be used to operate a local variable a value between -128 and 127. This code is compared with the subtraction operation for the variable control loop (for or while) performed more efficient. Like the add instruction, there is no exception is thrown, the overflow is ignored generally slightly.

The second row of Table 12-2 describes the wide variable iinc instructions. As discussed in Chapter 10, by using the wide instruction can be extended unsigned local variable index from 8-16. 16-bit instructions may index address up to 65,536 locations variable. In the processing of instructions, iinc, wide instructions are used to increment the value of the signed extension from 8-16. Thus, iinc wide opcode variable may -32768-- change the value of a local variable in the range of 32,767.

It is noted that the sequence of bytecodes manner described java source code boolean type structure in Zhan Java bytecode. Value stored in the local variable in position 2 represents boolean foundPrime variable in the source code, it is - int. By pressing it a person or an int instruction 0 is set to true or false constants; to check the value of the boolean instructions performed by comparing an int value 0.

 

Guess you like

Origin www.cnblogs.com/mongotea/p/11980070.html