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

This question already has answers here:
Closed 10 years ago.
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?

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.

It's meant to force a value to a boolean. So if the value is evaluating to something, you'll get true.

It forces a true value to be exactly 1.
In a boolean context (if (x), while (x), x ? a : b, for (; x; )), a value which is 0 means false, while any other value means true.
If your function accepts a truth value, but you need exactly 1 there, !! is fine.
In other words, !!x is the same as x ? 1 : 0.

Most probably continuousSpellCheckingEnabled is of type bool. BOOL is defined as int, so:
continuousSpellCheckingEnabled = enabled;
issues a warning, while:
continuousSpellCheckingEnabled = !!enabled;
does not.

why don't they directly use "enabled"
Double negation only cancels out for boolean values (0 and 1) so !!(0) == 0 for non-boolean values the value will be converted to boolean !!(100) == 1

Related

Understanding bitwise XOR (^) with boolean variables

I'm from a game programming background and I've just come across a bitwise XOR ^. I've seen examples of how it works with integers, but I'm a bit confused about the result with boolean values. I know a bool is either 0 or 1, but after testing I haven't been able to replicate the ^ result with simple operators. Could someone please explain to me what the following code snippet (specifically the ^) is doing? Many thanks.
bool body1awake = rigidbody1.isAwake;
bool body2awake = rigidbody2.isAwake;
if (body1awake ^ body2awake)
{
if (body1awake) rigidbody2.SetAwake();
else rigidbody1.SetAwake();
}
Exclusive or of two bits is true when only one of them is set. If both are set or not set then it is false. Since bool basically represents a single bit (0 or 1 are its only values)
if (body1awake ^ body2awake)
means that the condition will be true on when body1awake != body2awake.
As bool is a narrower type than an int, both arguments are implicitly converted to an int prior to the XOR being evaluated. true assumes the value 1, and false assumes the value 0.
If that result is non-zero then the if body runs, and that happens if and only if body1awake is not equal to body2awake.
So perhaps the equivalent
if (body1awake != body2awake)
would have been better. If the author thinks their way is faster then they need a stern talking to with compiler optimisations and as-if rule being introduced into the conversation.

What ! do when initializing a variable before the value [duplicate]

This question already has answers here:
What is !0 in C?
(5 answers)
Closed 3 years ago.
I was doing some random stuff and I came up with int a = !3; and when I outputted the value it I was expecting an error but it got me a 0.
Why did this happen and what ! means on that example?
! is the BOOLEAN NOT operator, i.e. !true == false and !false == true. In C and C++ every value that is nonzero is treated as true when used with a boolean operator. And false is numerically 0. So 3 is treated as true and !3 = !true = false = 0.
!3 is an expression, it evaluates to a bool type.
In this example it evaluates to false.
bool's can be casted to an int, which happens automatically when you assign it to one.
The int representation of false is '0' whilst true is '1'.
That "negates" the value. Anything non-zero becomes 0, and 0 becomes 1.

Why does "n&1 == 0" always return false? [duplicate]

This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Closed 6 years ago.
Why does the expression n&1 == 0 always return false, where n is an integer?
I want to use bitwise operation to determine whether n is even. However, it always return false. (The clion also prompted me that it always returns false).
What's more, it works when I use n&1 != 0 to determine whether n is odd.
Its because of the operator precedence.
== has higher precedence than the & operator, so 1 == 0 gets evaluated first to 0. Then the bit wise AND is performed which ultimately returns false.

What is the boolean value of integers other than 0 or 1?

I was writing a simple function to derive a new filename based on a set of files that should be named as cam1_0.bmp, cam1_1.bmp, and tried this out.
static int suffix = 0;
std::string fPre("cam");
std::ifstream fs;
std::string fName;
do {
fName = fPre;
fName.append(std::to_string(camera)).append("_").append(std::to_string(suffix)).append(".bmp");
fs.open(fName);
} while (fs.good() && ++suffix);
This works and it made me wonder what is the standard, defined behavior of corresponding boolean values for number values other than 0 or 1. With this experiment I know all values including negative values other than 0 evaluates to true. Is only 0 considered to be false as per the standard?
In C++, integers don't have boolean values. (Different languages have different rules, but this question is about C++.)
The result of converting an integer value to type bool (a conversion which is often done implicitly) is well defined. The result of converting 0 to bool is false; the result of converting any non-zero value to bool is true.
The same applies to floating-point values (0.0 converts to false, all other values convert to true) and to pointers (a null pointer converts to false, all non-null pointer values convert to true).
The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.
Source
Yes, any number other than 0 is considered as true for boolean.
Visit http://www.vbforums.com/showthread.php?405047-Classic-VB-Why-is-TRUE-equal-to-1-and-not-1 for some explanation.

What is the usage of "!!" (negating twice)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Double Negation in C++ code
Let's say:
bool var = !!true;
It will assign "true" to the variable. Seems useless, but I was looking at Visual Studio's definition of "assert", and it is:
#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
Why does it negate the "_Expression" twice?
I wonder that they want to force the "!" operator to be called (in the case it is overloaded), but that doesn't seem to be a good reason.
!! guarantees that the result will end up as a 1 or a 0, rather than just the value of _Expression or 0. In C, it's unlikely to matter, but in C++ I think it turns the result of the expression into a bool type, which might be useful in some cases. If you did have some API that required a literal 1 or 0 be passed to it, using !! would be a way to make it happen.
It's possible that you might want an int variable that's either 1 or 0.
So you can't for example pass a 5, instead the double negation would turn that 5 into a 1.
Also, have a look at how TRUE is defined:
#ifndef TRUE
#define TRUE 1
#endif
Therefore, an expression like:
int x = 5;
if ( x == TRUE )
{
//....
}
would not pass, whereas
if ( x )
{
//....
}
would.
Its use is to make sure the value is either 0 or 1. I think it's superfluous with C++'s bool type.