Why а=70 ?? I can’t find the order of actions in the formula:
int i = 1; int a = 3+i+6*++i*5^12>>1; System.out.println(i); //2 System.out.println(a); //70 Why а=70 ?? I can’t find the order of actions in the formula:
int i = 1; int a = 3+i+6*++i*5^12>>1; System.out.println(i); //2 System.out.println(a); //70 We look at the priority table of operations and place brackets in accordance with these priorities, given that operations of the same priority are performed from left to right.
The lowest priority among the available operators has an "exclusive or" (xor) ^ , so the expression is equivalent to the following:
a = (3 + i + 6 * ++i * 5) ^ (12 >> 1); Further the expression in the first brackets will be calculated, i.e .:
3 + i + 6 * ++i * 5 It will be calculated as follows:
1) 3 2) [_1_] + i == 4 3) 6 4) ++i == 2 и i := 2 5) [_3_] * [_4_] == 6 * 2 == 12 6) [_5_] * 5 == 12 * 5 == 60 7) [_2_] + [_6_] == 4 + 60 == 64 (Here [_N_] is the value calculated in step N )
Further it is calculated
12 >> 1 == 6 and finally
a = 64 ^ 6 = 70 Based on the priorities of the operators, the order will be as follows:
1) ++
2) *
3) +
4) >>
5) ^
6) =
Let's see how the sequential execution of statements in the above code looks like:
1) 3 + 1 + 6 * 2 * 5 ^ 12 >> 1 (i after the increment is 2)
2) 3 + 1 + 12 * 5 ^ 12 >> 1
3) 3 + 1 + 60 ^ 12 >> 1
4) 4 + 60 ^ 12 >> 1
5) 64 ^ 12 >> 1
6) 64 ^ 6
7) 70
8) well, and operator =
As you can see, that's right, i = 2, a = 70
3+1+12*5^12>>1 and not 3+2!!!+12*5^12>>1 - jack1+2*3 first action is the calculation of expression 1 , the second is expression 2 , the third is expression 3 , the fourth is the multiplication of the second and third, the fifth is the addition of the first and fourth. - user194374Source: https://ru.stackoverflow.com/questions/629585/
All Articles