Home >Backend Development >C++ >Puzzle on C/C++ R-Value expression?

Puzzle on C/C++ R-Value expression?

WBOY
WBOYforward
2023-09-15 18:05:021098browse

C/C++ R-Value表达式上的谜题?

Here we will see a puzzle. Suppose there is a program like below, we have to tell what is output and why?

Example

#include<iostream>
using 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

Output

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 -

Example

#include<iostream>
using namespace std;
int main() {
   int x = 0xab;
   x = ~x;
   cout << hex << x;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete