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 .:-)
Jonathan gilbert
source share