Here we will see a puzzle. Suppose there is a program like below, we have to tell what is output and why?
#includeusing namespace std; int main() { int x = 0xab; ~x; cout << hex << x; }
So what is the output? ~x is performing a complement operation. So does it display the two's complement result in hex?
The output is as follows
ab
So, there is no change. but why? The reason is that ~x is converting x to its complement form, but the value is not assigned to any variable. This expression is an R-value expression. The lvalue is not stored into some variable until it is used. If we enter the L value it will look like this -
#includeusing namespace std; int main() { int x = 0xab; x = ~x; cout << hex << x; }
ffffff54
The above is the detailed content of Puzzle on C/C++ R-Value expression?. For more information, please follow other related articles on the PHP Chinese website!