1

Possible Duplicate:
Double Negation in C++ code

While reading one code I read:

flush = ! !(flags & GST_SEEK_FLAG_FLUSH);

I am not getting what does !! mean here . what does this sentence do?

EDIT:

I got it its a double-negative. trick to convert non-bool data to bool

But what is the need of that? Here flush is bool then if you assign any non zero item to bool it will treat as 1 and zero item as 0 so whats benefit of doing this?

Community
  • 1
  • 1
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • Maybe someone confused the logical NOT (`!`) with the bitwise NOT (`~`)? – Uwe Keim Dec 15 '11 at 06:02
  • but whats need of that?? here flush is bool then if you assign any non zero item to bool it will treat as 1 and zero iteam as 0 so whats benefit of doing this? – Jeegar Patel Dec 15 '11 at 06:22
  • @Mr.32 It is used to check whether the `GST_SEEK_FLAGS_FLUSH` bit is set in the `flags` variable. – Uwe Keim Dec 15 '11 at 06:30
  • 1
    This might be a duplicate, but *not of that question*. That is C++, which has these funny extra data-types. This is C. –  Dec 15 '11 at 06:33
  • 1
    @Mr.32: Well bool wasn't always available. In C versions before c99 there was no `bool`. It was just an `int` which could have any number in it. So if `flush` would still be an int then the result of `(flag & GST_SEEK_FLAG_FLUSH)` might have the values 0 or `GST_SEEK_FLAG_FLUSH`. And `GST_SEEK_FLAG_FLUSH` could be `1` but it could also be `4`. Now if you want to ensure that the result of that expression is either `0` or `1` while `flush` being an int you could either write `(flags & GST_SEEK_FLAG_FLUSH) != 0` or what you have seen `!!(flags & GST_SEEK_FLAG_FLUSH)`. – vstm Dec 15 '11 at 06:36

4 Answers4

9

It's a double-negative. It's a way of converting an otherwise-non-bool expression (such as flags & GST_SEEK_FLAG_FLUSH) to a bool. I personally prefer:

flush = (flags & GST_SEEK_FLAG_FLUSH) != 0;

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
  • but whats need of that?? here flush is bool then if you assign any non zero item to bool it will treat as 1 and zero iteam as 0 so whats benefit of doing this? – Jeegar Patel Dec 15 '11 at 06:22
  • 1
    @Mr.32 Perhaps later on ... `if (1 == flush) { ... }`. Because sometimes "bool conditional checking" is done explicitly with assumptions (and we all know that assumptions... ;-), not implicitly like `if (flush) { ... }`. Also, `flush` could be used in an entirely different context where it *must* be 1/0 to reflect a valid value, so the previous was just one example. "Better safe than sorry", and being consistent will minimize the "sorry" later :) –  Dec 15 '11 at 06:27
  • Yep, it's just a guarantee that it's a legitimate bool value rather than just simply being a non-zero value. It obfuscates the code, though, so I agree that it looks like nonsense, hence why I prefer the version I posted. It correctly turns it into a bool, and it *looks* like the way the brain is expecting it. – Jim Buck Dec 15 '11 at 06:55
2

Just wanted to add a little example for you that might clear things.

 main()
 {
 int i=10;
 printf("%d",!!i);
 }

Output is 1

Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • but whats need of that?? here flush is bool then if you assign any non zero item to bool it will treat as 1 and zero iteam as 0 so whats benefit of doing this? – Jeegar Patel Dec 15 '11 at 06:23
2

If flush is boolean variable, and you force some non-boolean value to it, some compiler will generate a warning forcing value to bool 'true' or 'false'. So it's safer to use double negation.

jcisio
  • 509
  • 1
  • 7
  • 17
2

Some compilers (like Visual Studio) will warn when coercing a non-boolean type to an int, for example:

#include <stdio.h>

int main(void) {
    int x = 10;
    bool y = x; // warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    return 0;
}

The double-negation trick is one way of converting to bool and preventing this warning, however I would lean towards the != 0 check Jim Buck recommended for clarity.

DRH
  • 7,868
  • 35
  • 42