39

I reading this code, and have this line

 switch (!!up + !!left) {

what is !! operator ? two logical NOT ?

9
  • Yup. Sounds like whoever wrote that didn't quite know what they were doing.
    – AndyG
    Apr 24, 2012 at 23:13
  • 5
    Did you think to, you know, try it and find out? Apr 24, 2012 at 23:14
  • 3
    @SauceMaster: On the contrary. The double not trick is used very often. The Linux kernel source has some uses of it, and I'm pretty sure that the kernel hackers know what they are doing.
    – C2H5OH
    Apr 24, 2012 at 23:15
  • 11
    It's the logical-yes operator. Apr 24, 2012 at 23:25
  • 5
    @weston (up || left) will yield 0 or 1, whereas (!!up + !!left) will yield 0, 1, or 2.
    – kittemon
    Apr 27, 2012 at 17:17

4 Answers 4

59

yes, it's two nots.

!!a is 1 if a is non-zero and 0 if a is 0

You can think of !! as clamping, as it were, to {0,1}. I personally find the usage a bad attempt to appear fancy.

4
  • 18
    not to be confused with a double knot, which is hard to get out of. Apr 24, 2012 at 23:14
  • 6
    It's not so much "clamping" as it is "normalizing".
    – kittemon
    Apr 24, 2012 at 23:18
  • 9
    If you don't like it, what would you prefer? (_Bool) (or (bool) if stdbool.h has been included) works on C99 but it seems gratuitously dependent on C99, and I find a lot fewer people understand how cast-to-bool works than understand the !! idiom. The other alternative, expr?1:0, is just ugly and gratuitously verbose. Apr 25, 2012 at 0:06
  • "a bad attempt to appear fancy" 💕
    – Rob
    Apr 14, 2020 at 18:43
15

You can imagine it like this:

!(!(a))

If you do it step by step, this make sense

result = !42;    //Result = 0
result = !(!42)  //Result = 1 because !0 = 1  

This will return 1 with any number (-42, 4.2f, etc.) but only with 0, this will happens

result = !0;    //Result = 1
result = !(!0)  //result = 0
8

!! is a more-portable (pre-C99) alternative to (_Bool).

4

You're right. It's two nots. To see why one would do this, try this code:

#include <stdio.h>

int foo(const int a)
{
    return !!a;
}

int main()
{
    const int b = foo(7);
    printf(
        "The boolean value is %d, "
        "where 1 means true and 0 means false.\n",
        b
    );
    return 0;
}

It outputs The boolean value is 1, where 1 means true and 0 means false. If you drop the !!, though, it outputs The boolean value is 7, where 1 means true and 0 means false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.