Confused by use of double logical not (!!) operator [duplicate]

It is not as simple as double negation. For example, if you have x == 5, and then apply two ! operators (!!x), it will become 1 – so, it is used for normalizing boolean values in {0, 1} range.

Note that you can use zero as boolean false, and non-zero for boolean true, but you might need to normalize your result into a 0 or 1, and that is when !! is useful.

It is the same as x != 0 ? 1 : 0.

Also, note that this will not be true if foo is not in {0, 1} set:

!!foo == foo

#include <iostream>

using namespace std;

int main()
{
        int foo = 5;

        if(foo == !!foo)
        {
                cout << "foo == !!foo" << endl;
        }
        else
        {
                cout << "foo != !!foo" << endl;
        }



        return 0;
}

Prints foo != !!foo.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)