3- integer arithmetic

Integer arithmetic 
integer arithmetic Java to follow four rules of arithmetic, you can use any nested parentheses. Four operations consistent with the rules and elementary mathematics. For example: 
        int I = ( 100 + 200 is ) * ( 99 - 88 ); // 3300 
        int n-= . 7 * ( . 5 + (I - . 9 )); // 23072 

but always precise integer operations, even if the division is precise, because only two integers obtained by dividing the integer part 
int X = 12345 / 67 ; // 184 

Note: integer of the divisor for division error run 0:00, but not compile error. 

Using modulo arithmetic   % :
 int Y = 12345 % 67 ; //12345 ÷ 67 remainder 17 is 

also a shorthand operator, i.e., + =, - =, * =, / = , they are used as follows:
 int n-+ = 100 ; // corresponding to n = n + 100; 
int m - = 100 ; // corresponds to m = m - 100; 


overflow 
integer range limitation due to the presence, if the calculation result is out of range, then an overflow, overflow without error, but it will give a surprising result: 
        int = X 2147483640 ;
         int Y = 15 ;
         int SUM = X + Y; // -2,147,483,641 

to explain these results, we integer 2147483640 and 15 into binary adder do: 
 0111  1111  1111  1111  1111  1111  1000 
+ 0000  0000 0000  0000  0000  0000  1111 
------------------------------------ 1000 0000 0000 0000 0000 0000 0111 
Since the highest positioning calculation result is 1, therefore, the addition result becomes a negative number. 
The solution to this problem is very simple, the x and y of type int into a larger Long . 
Increment / decrement 
Java also provides ++ operator and - computing, which can be operated plus 1 and minus 1 to an integer. 
Note ++ / - EDITORIAL and back calculation results are different, n + 1 represents the first increase and then referenced n, n + 1 represents a first reference plus n. Not recommended ++ operator incorporated into routine operation, easy to put yourself out himself senseless.
int age = . 6 ;
 // first incremented and then use (before age 1 from Canada, and then print age = 7, 7 at this time is a value for age in memory) 
the System. OUT .println ( "
        

= age " + ++ age); 

// start with, and then adding the self
 // At this age value is 7 in the memory, prints age = 7, then adding an age since then,
 // so in this case the print age = 7, but in fact, at this time the value of age is already in memory 8 the 
System. oUT .println ( " age = " + age ++ ); 

// at this print is out = 8 age 
System. oUT .println ( " age = " + Age); 

int X = 100 + (++ Age); // do not use such a 


shift operation 
in the computer, integer always expressed in binary form, for example,. int integer type 7 uses 4 bytes binary as follows:
 00000000  0000000  0000000 00000111 
// left 
int n-= . 7 ;        // 00000000 00000000 00000000 00000111 
int A = n-<< . 1 ;   // 00000000 00000000 00000000 00001110 <= 14 // left a 2 * 
int B << = n- 2 ;   / / 00000000 00000000 00000000 00011100 <= 28 
int C << = n- 28 ; // 01110000 00000000 00000000 00000000 <= 1,879,048,192 
int D << = n- 29 ; // 11100000 00000000 00000000 00000000 <= -536 870 912
 // 29 Rotate left Since the most significant bit becomes 1, and therefore the results become negative.
// right 
int m = . 7 ;        // 00000000 00000000 00000111 0000000 
int X = m >> . 1 ;   // 00000000 00000000 00000011 0000000 <=. 3 
int Y >> m = 2 ;   // 00000000 00000000 00000001 0000000 <=. 1 
int >> m = Z . 3 ;   // 00000000 00000000 00000000 0000000 <= 0 

if a negative number to the right, most significant bit to 1 does not move, the result is still a negative:
 int n-= - 536870912 ;
 int a >> = n- 1 ;   //00000000 00000000 00000000 11110000 <= -268 435 456 
int B = n->> 2 ;   // 10.111 million 00000000 00000000 00000000 <= -134 217 728 
int C = n->> 28 ; // 10000000 00000000 00000000 00000001 <-2 = 
int D = n->> 29 ; // 1,000,000,000,000,000 00000000 00000000 <= -1 

there is a unsigned right shift operation using >>> , also called a logical right shift, if the number is positive, the high bit of 0, and if the number is negative , the same high level after the right complement 0:
 int n-= - 536870912 ;
 int A = n->>> . 1 ;   // 01110000 00000000 00000000 00000000 <= 1879048192 
int B = n->>>2;   // 00111000 00000000 00000000 00000000 <= 939 524 096 
int C = >>> n- 29 ; // 00000000 00000000 00000000 00000111 <=. 7 
int D = n->>> 31 is ; // 00000000 00000000 00000000 00000001 <=. 1 

Note: The byte and short -time type shift, will first be converted to int further displacement. 

Bitwise 
bit operation is performed bitwise AND, OR, XOR, and non-operation. 
// and have 0 0, the same one. 1 
n-= 0 & 0 ; // 0 
n-= 0 & . 1 ; // 0 
n-= . 1 &0 ; // 0 
n-= 1 & 1 ; // 1
 // or a 1, with 0 0 
n-= 0 | 0 ; // 0 
n-= 0 | 1 ; // 1 
n-= 1 | 0 ; // 1 
n-= 1 | 1 ; // 1
 // than 0 and 1 into 
n-~ = 0 ; // 1 
n-~ = 1 ; // 0
 //0 is the same or different, is different. 1 
n-= 0 ^ 0 ; // 0 
n-= 0 ^ . 1 ; // . 1 
n-= . 1 ^ 0 ; // . 1 
n-= . 1 ^ . 1 ; // 0 

two integers bit computing, in fact, bit-aligned, and then turn on each bit computing. For example: 
int I = 167 776 589 ; // 00001010 00000000 00010001 01001101 
int n-= 167 776 512 ; // 00001010 00000000 00010001 00000000 
the System. OUT .println (n-I &);   //167 776 512 


operations a priority 
in Java calculation expression, the calculation priority from high to low is: 
()
 ~ ++ -! 
* /% 
+ - 
<< >> >>> 
& 
| 
+ = - = * = / = 


type automatic lifting and casts 
in the arithmetic process, inconsistencies if two types involved in computing the number, the larger the calculation result of the type integer. 
For example, short and int calculation, the result is always int , because the short first transition automatically int :
         short S = 1234 ;
         int I = 123456 ;
         int X = S + I; // S transition automatically int 
        short Y = S i +; // ! compile error 

will result casts, soon transformed into a wide range of integer range of small integers. Casts using (type), for example, the int as castsShort :
 int I = 12345 ;
 Short S = ( Short ) I; // 12345 

Note: out of range casts get the wrong result, because when the transformation, int upper two bytes will be directly thrown away, only retains the lower two bytes:
         int I1 = 1234567 ;
         Short S1 = ( Short ) I1; // -10 617 
        int I2 = 12345678 ;
         Short S2 = ( Short ) I2; // 24910

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417407.html