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

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

WBOY
Release: 2023-09-15 18:05:02
forward
1102 people have browsed it

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;
}
Copy after login

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
Copy after login

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;
}
Copy after login

Output

ffffff54
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template