The meaning of double exclamation mark in C++

When looking at some codes, I sometimes see the usage of !!(cond) . What does this usage mean?

Answer: We all know the meaning of !(cond), which is the meaning of negation. If cond is non-0, the value is 0; if cond is 0, the value is 0;

Then by analogy, it is not difficult to figure out the meaning of !! , that is , if cond is non-0, the value is 1; if cond is 0, the value is 1 .

There is a hidden meaning here, that is, after modification with !!, the result obtained must be 0 or 1, and the purpose of modifying the value can be achieved, for example:

int k = 12;
int m = -1;
int n = 0;

cout<<"!!k = "<<!!k << " !!m = "<< !!m << " !!n = "<<!!n;

输出结果为:
!!k = 1 !!m = 1 !!n = 0

From the above example, you can see that the original values ​​of k and m were 12 and -1 respectively, but after modification, they became 1.

Note: Not recommended!! Modify floating point numbers .

Guess you like

Origin blog.csdn.net/iqanchao/article/details/132980598