Re: What does !! do?

Discussion in 'C++' started by James Kanze, Feb 24, 2013.

  1. James Kanze

    James Kanze Guest

    On Thursday, January 24, 2013 9:11:06 AM UTC, David Brown wrote:
    > On 23/01/13 16:24, Bint wrote:


    > > I haven't seen this syntax before. What does "!!" do in C++?


    > > As in this statement


    > > int t = !!(x & size);


    > Think of it as a boolean "normalize" operator that will turn any
    > non-zero "true" value into 1, and leave zero "false" as 0.


    There is no such thing as a boolean "normalize" operator. An
    expression which has type bool can only have one of two values:
    false and true (*not* 0 and 1). The ! operator takes a bool,
    and returns a bool. In the above expression, there is an
    implicit conversion of "x & size" to bool; such implicit
    conversions are simply obfuscation, and should be made explicit:

    bool t = (x & size) != 0;

    or if you really need an int, with values 0 and 1:

    int t = (x & size) != 0 ? 1 : 0;

    or

    int t = static_cast<int>( (x & size) != 0 );

    (But I've never actually encountered such a case.)
    James Kanze, Feb 24, 2013
    #1
    1. Advertising

  2. James Kanze

    Balog Pal Guest

    On 2/24/2013 6:38 PM, James Kanze wrote:

    >> Think of it as a boolean "normalize" operator that will turn any
    >> non-zero "true" value into 1, and leave zero "false" as 0.


    > There is no such thing as a boolean "normalize" operator. An
    > expression which has type bool can only have one of two values:
    > false and true (*not* 0 and 1).


    And if you work with types that are not 'bool' but still boolean and in
    the quote, that is why the normalisation is needed.

    Guess you agree that we have abundant amount of code out there that
    creates typedef to some boolean(-intended) type that maps to some other
    integral type. Say for compatibility with old C.

    And those can store other values, that are waiting to cause headache.
    Balog Pal, Feb 24, 2013
    #2
    1. Advertising

  3. James Kanze

    James Kanze Guest

    On Sunday, 24 February 2013 19:41:39 UTC, Balog Pal wrote:
    > On 2/24/2013 6:38 PM, James Kanze wrote:


    > >> Think of it as a boolean "normalize" operator that will turn any
    > >> non-zero "true" value into 1, and leave zero "false" as 0.


    > > There is no such thing as a boolean "normalize" operator. An
    > > expression which has type bool can only have one of two values:
    > > false and true (*not* 0 and 1).


    > And if you work with types that are not 'bool' but still boolean and in
    > the quote, that is why the normalisation is needed.


    > Guess you agree that we have abundant amount of code out there that
    > creates typedef to some boolean(-intended) type that maps to some other
    > integral type. Say for compatibility with old C.


    > And those can store other values, that are waiting to cause headache.


    Even then, you'ld write something like "x != 0" (which is
    guaranteed to return 0 or 1 in old C), rather than "!!x" (which
    is guaranteed to confuse any reasonable person---"!!" is
    a double negative: the second ! negates the first).

    --
    James
    James Kanze, Feb 25, 2013
    #3
  4. James Kanze

    Rui Maciel Guest

    Balog Pal wrote:

    > On 2/24/2013 6:38 PM, James Kanze wrote:
    >
    >>> Think of it as a boolean "normalize" operator that will turn any
    >>> non-zero "true" value into 1, and leave zero "false" as 0.

    >
    >> There is no such thing as a boolean "normalize" operator. An
    >> expression which has type bool can only have one of two values:
    >> false and true (*not* 0 and 1).

    >
    > And if you work with types that are not 'bool' but still boolean and in
    > the quote, that is why the normalisation is needed.


    If those types are user-defined, isn't it possible to achieve the same
    effect by providing conversion functions?


    Rui Maciel
    Rui Maciel, Feb 25, 2013
    #4
  5. James Kanze

    Balog Pal Guest

    On 2/25/2013 1:24 PM, James Kanze wrote:

    >> And those can store other values, that are waiting to cause headache.

    >
    > Even then, you'ld write something like "x != 0"


    I did that for some time then switched to the other in like mid 90's.

    > (which is guaranteed to return 0 or 1 in old C)


    Err, isn't !! guaranteed to return that too? I admittedly not very well
    versed in pre-ANSI C, but am positive its result is mandatory 0 or 1
    depending on how the operand originally compares to 0.

    >, rather than "!!x" (which
    > is guaranteed to confuse any reasonable person---"!!"


    Sure term "reasonable person" is something subjective but I have about 2
    decades of real experience that it did not confuse anyobne around me for
    more than a few minutes. And after the first coin drop everyone used it
    without problems on either reading or writing.

    > is a double negative: the second ! negates the first).


    In theory probably, but in practice it is read like a pattern -- like it
    was a single operator. Same way you recognise a ton of
    styles/idioms/whatever in code and learn to understand or avoid them.

    As I said before I see no reason to ever see it in code that uses
    "native" booleans like bool or _Bool, but pretty natural with BOOL of
    WIN32 API (that is actually a 32 bit int, and gladly picks up any
    pattern from type-punned things like WPARAM, LPARAM, LRESULT, or
    handles/pointers floating around. (As a cherry on the cake there's also
    MSVC's level 1 sillywarning about performance of converting int to
    bool... ;)
    Balog Pal, Feb 25, 2013
    #5
  6. James Kanze

    Balog Pal Guest

    On 2/25/2013 1:43 PM, Rui Maciel wrote:
    > Balog Pal wrote:
    >
    >> On 2/24/2013 6:38 PM, James Kanze wrote:
    >>
    >>>> Think of it as a boolean "normalize" operator that will turn any
    >>>> non-zero "true" value into 1, and leave zero "false" as 0.

    >>
    >>> There is no such thing as a boolean "normalize" operator. An
    >>> expression which has type bool can only have one of two values:
    >>> false and true (*not* 0 and 1).

    >>
    >> And if you work with types that are not 'bool' but still boolean and in
    >> the quote, that is why the normalisation is needed.

    >
    > If those types are user-defined, isn't it possible to achieve the same
    > effect by providing conversion functions?


    The "user" that defines them is the provider of the API or library (like
    WIN32), and majority is typedef for some built-in integral type.
    Balog Pal, Feb 25, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Johnson
    Replies:
    0
    Views:
    1,225
    Bill Johnson
    Jul 8, 2005
  2. Replies:
    3
    Views:
    465
    Ken Cox [Microsoft MVP]
    Oct 7, 2005
  3. Janet Collins
    Replies:
    0
    Views:
    1,128
    Janet Collins
    Jan 13, 2006
  4. Replies:
    1
    Views:
    827
    Juan T. Llibre
    Feb 8, 2006
  5. Alek Nazarian
    Replies:
    7
    Views:
    3,206
    Alek Nazarian
    Oct 22, 2003
Loading...

Share This Page