Double not operator? (!!)

Started by
26 comments, last by Zipster 15 years, 3 months ago
Quote:Original post by _moagstar_
Quote:Original post by Xai
That is silly - or possibly worse, stupid.


I think this idiom is actually trying to be a bit too clever.


I don't know, I think it's useful and perfectly clear when used on primitive types. For example:

void Foo(int x, int y){    if(!!x ^ !!y)    {        // x is nonzero and y is zero, or y is nonzero and x is zero    }}
NextWar: The Quest for Earth available now for Windows Phone 7.
Advertisement
Quote:Original post by Sc4Freak
void Foo(int x, int y){    if(!!x ^ !!y)    {        // x is nonzero and y is zero, or y is nonzero and x is zero    }}


Well dropping the !! there makes much more sense to me, as it's just a regular XOR..
GBS
Quote:Original post by Sc4Freak
I don't know, I think it's useful and perfectly clear when used on primitive types. For example:
My definition of "perfectly clear" is "I get it when I read it", which certainly isn't the case for "!!x ^ !!y". Especially compared to "(x == 0) != (y == 0)".

Quote:Original post by Sc4Freak
I don't know, I think it's useful and perfectly clear when used on primitive types.


Maybe that's a matter of taste, but I would say it's probably better to write code in the simplest and clearest manner possible, so that as many people can understand it as possible. Personally I find (var != 0) much clearer than !!var. If I read the code in my head I hear myself saying 'var is not equal to zero' and 'not not var' respectively.
With primitive types (prim!=0) is much more clear than (!!prim). However, is it faster?
-----------------------------------------Everyboddy need someboddy!
Quote:Original post by someboddy
With primitive types (prim!=0) is much more clear than (!!prim). However, is it faster?


In terms of compiled code they are precisely the same, however in terms of performance this is the last thing you should be worrying about, my advice would be to just pick whichever you find clearest and stick with it.
The purpose of this idiom really has very little to do with safe-bool. It's mostly to interop with libraries written to be compatible with C. Older versions of C did not have a true boolean type and instead used the convention that 0 meant false and not-0 meant true. "not-0" literally means "anything that isn't 0". So for example 8 is considered to be true. The idiom came about because some compilers (notably MSVC) will complain about assigning an int to a bool even though it will work fine.

Personally I find "!!foo" to be easier to understand than "foo!=0" or "(bool)foo". "!!foo" means "foo is a C-style boolean and I'm converting it to a C++ style boolean". "foo!=0" implies to me that foo is actually an integer, not a boolean. "(bool)foo" just looks like something wierd is going on.
-Mike
And to add to Anon Mike's post, because this is primarily a C idiom, writing C++ patches around it is rather misguided. C++ has a proper boolean type, amonst other things.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by GraphicsBas
Well dropping the !! there makes much more sense to me, as it's just a regular XOR..

That wouldn't preserve the meaning. ^ is a bitwise xor, not a logical xor.
Quote:Original post by GraphicsBas
Quote:Original post by Sc4Freak
void Foo(int x, int y){    if(!!x ^ !!y)    {        // x is nonzero and y is zero, or y is nonzero and x is zero    }}


Well dropping the !! there makes much more sense to me, as it's just a regular XOR..


XOR is a bitwise operator. Consider carefully what happens if x == 2 and y == 1.

This topic is closed to new replies.

Advertisement