Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use two '!' operators on a boolean value? [duplicate]

Tags:

c++

boolean

Possible Duplicate:
Double Negation in C++ code

When I scanned the Webkit source code, I found a strange use of the boolean "not" operator !:

BOOL enabled;
if (SUCCEEDED(sharedPreferences->continuousSpellCheckingEnabled(&enabled)))
    continuousSpellCheckingEnabled = !!enabled;
if (SUCCEEDED(sharedPreferences->grammarCheckingEnabled(&enabled)))
    grammarCheckingEnabled = !!enabled;

Why don't they use enabled directly instead of !!enabled?

like image 709
Jiajun Avatar asked Dec 12 '12 08:12

Jiajun


People also ask

Is 2 True or false boolean?

That treats 2 as a boolean value (which is considered true). To ask if x is 1 or 2, say x ==1 || x == 2. In English, is sometimes means "is equal to", and sometimes means "has the property".

Does a boolean only have two values?

A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program.

What is the difference between and ||? In r?

Show activity on this post. According to the R language definition, the difference between & and && (correspondingly | and || ) is that the former is vectorized while the latter is not.


1 Answers

It's a C++ trick to convert anything to 1 or 0.

For example, 42 is logically true, but is not 1, so applying !! to it, you turn it into 1.

like image 83
Luchian Grigore Avatar answered Oct 24 '22 10:10

Luchian Grigore