Priority ~ and ++ in java

consider this piece of code

int j = 7; System.out.println(Integer.toBinaryString(j)); j = ~j++; System.out.println(Integer.toBinaryString(j)); 

prints

 111 11111111111111111111111111111000 

what i expect to see

 111 11111111111111111111111111111001 

At first I thought it might be a priority ~ and ++

if ~ is evaluated before ++ the answer will be

 11111111111111111111111111111001 

else if ++ evaluates to ~

 11111111111111111111111111110111 

I was looking for Oracle tutorials , but I could not find the answer. Can anyone explain this behavior?

+7
source share
5 answers

Remember that the "++" post-increment operator returns j before the increment occurs. That is, if "j" is 7, then "j ++" sets j to 8, but returns 7. ~ 7 is the result you saw, a number ending in three 0 bits.

The post-increment operator "++" can only work on so-called "L-values." An L-value is a value that actually exists somewhere that the code can logically refer to - a variable or an array element, a parameter, or a class field. As soon as you take the value of the L-value and apply some numerical operation to it, you get the value of R. The values โ€‹โ€‹of R are just a value, and they do not belong to the permanent store where the result can be set. You can assign L values, but not R values, and therefore, if you try to assign the value "++" R, you will get a compilation error.

If the โ€œ~โ€ operator went first, then you would set the value to R, as in (~ j) ++. This did not compile. The fact that compiling code in general means that priority is a different way: ~ (j ++).

Brackets like this are the easiest way that I know that you can sort the priority whenever there is any confusion: just write three test cases:

  • An original way that you are not sure.
  • With parentheses forcing the same order of operations.
  • With parentheses that force a different order of operations.

Run it and see if # 2 or # 3 gives the same result as # 1 .:-)

+6
source

The code seems very fragile. I think that what happens is that when evaluating the expression "~ i ++" the value "~ i" is returned, "i ++" is executed, and then, finally, the assignment (overriding the previous value from "i ++").

+1
source

Both operators are correct associative operators of the second degree, but a simple test shows that it runs ++ first, and the BITWISE NOT operation last.

 int j = 7, z = 7, k = 7; z = z++; z = ~z; k = ~k++; j = ~j; j++; System.out.println(Integer.toBinaryString(z)); // 11111111111111111111111111111000 System.out.println(Integer.toBinaryString(j)); // 11111111111111111111111111111001 System.out.println(Integer.toBinaryString(k)); // 11111111111111111111111111111000 
+1
source

Unary operators ( ++1 , -- , + , - , ~ ! ) Are evaluated from right to left. Therefore, ++ evaluates to ~ .

+1
source

First, do not do this. There is no reason to associate such operators with a single expression, but I understand that you really did not plan to use this code in live code and just experimented. Try this page for Java operator priority: http://bmanolov.free.fr/javaoperators.php

0
source

All Articles