Re: What does !! do?

Discussion in 'C++' started by Victor Bazarov, Jan 23, 2013.

  1. On 1/23/2013 10:24 AM, Bint wrote:
    > I haven't seen this syntax before. What does "!!" do in C++?


    It does ! (logical NOT) twice.

    >
    > As in this statement
    >
    > int t = !!(x & size);


    It's shorthand for

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

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 23, 2013
    #1
    1. Advertising

  2. Victor Bazarov

    Ian Collins Guest

    Scott Lurndal wrote:
    > Victor Bazarov <> writes:
    >> On 1/23/2013 10:24 AM, Bint wrote:
    >>> I haven't seen this syntax before. What does "!!" do in C++?

    >>
    >> It does ! (logical NOT) twice.
    >>
    >>>
    >>> As in this statement
    >>>
    >>> int t = !!(x & size);

    >>
    >> It's shorthand for
    >>
    >> int t = (x & size) != 0;

    >
    > Which is longhand for
    >
    > int t = (x & size);


    No, it isn't.

    If x and size are both 42, t will be 42, not 1.

    --
    Ian Collins
     
    Ian Collins, Jan 23, 2013
    #2
    1. Advertising

  3. Victor Bazarov

    Jorgen Grahn Guest

    On Wed, 2013-01-23, Ian Collins wrote:
    > Scott Lurndal wrote:
    >> Victor Bazarov <> writes:
    >>> On 1/23/2013 10:24 AM, Bint wrote:
    >>>> I haven't seen this syntax before. What does "!!" do in C++?
    >>>
    >>> It does ! (logical NOT) twice.
    >>>
    >>>>
    >>>> As in this statement
    >>>>
    >>>> int t = !!(x & size);
    >>>
    >>> It's shorthand for
    >>>
    >>> int t = (x & size) != 0;

    >>
    >> Which is longhand for
    >>
    >> int t = (x & size);

    >
    > No, it isn't.
    >
    > If x and size are both 42, t will be 42, not 1.


    Another favorite is when there's a loss of precision in the assignment
    to t. 'x & size' may be non-zero, yet t is not.

    Missed such a bug in a review recently. Someone said "drop that
    unneccessary '!= 0'", I happily agreed, and then fortunately the bug
    showed up in testing. (This was in old C, with a homegrown boolean
    typedef -- unsigned char to "save space").

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jan 23, 2013
    #3
  4. Victor Bazarov

    Öö Tiib Guest

    On Wednesday, 20 February 2013 01:21:48 UTC+2, Bint wrote:
    > > On 1/23/2013 10:24 AM, Bint wrote:
    > >> I haven't seen this syntax before. What does "!!" do in C++?

    > >
    > > It does ! (logical NOT) twice.
    > >
    > >>
    > >> As in this statement
    > >>
    > >> int t = !!(x & size);

    > >
    > > It's shorthand for
    > >
    > > int t = (x & size) != 0;

    >
    > Forgive my ignorance but what does THAT do, the longer statement? AND'ing
    > two values together would give you a value ... What does appending the != 0
    > do?


    Compares the anded together value with zero. t will be 0 if it is zero and 1
    if it is not zero.

    Do not you have any book about C++ syntax so you have to ask it from us
    letter-by-letter? There are probably plenty of such ... even online in
    internet.
     
    Öö Tiib, Feb 19, 2013
    #4
  5. Bint <> wrote:
    >> On 1/23/2013 10:24 AM, Bint wrote:
    >>> I haven't seen this syntax before. What does "!!" do in C++?

    >>
    >> It does ! (logical NOT) twice.
    >>
    >>>
    >>> As in this statement
    >>>
    >>> int t = !!(x & size);

    >>
    >> It's shorthand for
    >>
    >> int t = (x & size) != 0;
    >>
    >> V

    >
    > Forgive my ignorance but what does THAT do, the longer statement? AND'ing
    > two values together would give you a value ... What does appending the != 0
    > do?


    That's again a shorthand for:
    int t = (x & size) != 0 ? 1 : 0;

    Which is shorthand for:
    int t = x & size; // that's _binary_ AND, not logical, so t can be
    arbitrary int
    if (x != 0)
    t = 1;
    else
    t = 0;

    Tobi
     
    Tobias Müller, Feb 20, 2013
    #5
  6. Tobias Müller <> wrote:
    > Bint <> wrote:
    >>> On 1/23/2013 10:24 AM, Bint wrote:
    >>>> I haven't seen this syntax before. What does "!!" do in C++?
    >>>
    >>> It does ! (logical NOT) twice.
    >>>
    >>>>
    >>>> As in this statement
    >>>>
    >>>> int t = !!(x & size);
    >>>
    >>> It's shorthand for
    >>>
    >>> int t = (x & size) != 0;
    >>>
    >>> V

    >>
    >> Forgive my ignorance but what does THAT do, the longer statement? AND'ing
    >> two values together would give you a value ... What does appending the != 0
    >> do?

    >
    > That's again a shorthand for:
    > int t = (x & size) != 0 ? 1 : 0;
    >
    > Which is shorthand for:
    > int t = x & size; // that's _binary_ AND, not logical, so t can be
    > arbitrary int
    > if (x != 0)
    > t = 1;
    > else
    > t = 0;
    >
    > Tobi


    Sorry, typos and formatting problems in the last example. Should be:

    int t = x & size; // that's _binary_ AND, not logical,
    // so t can be arbitrary int
    if (t != 0)
    t = 1;
    else
    t = 0;

    Tobi
     
    Tobias Müller, Feb 20, 2013
    #6
  7. Victor Bazarov

    army1987 Guest

    On Tue, 19 Feb 2013 17:21:48 -0600, Bint wrote:

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


    > Forgive my ignorance but what does THAT do, the longer statement?
    > AND'ing two values together would give you a value ... What does
    > appending the != 0 do?


    You're probably thinking about the logical AND operator, which is && (two
    ampersands); & (a single ampersand) is the bitwise AND operator (look it
    up).

    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
     
    army1987, Feb 20, 2013
    #7
  8. Victor Bazarov

    James Kanze Guest

    On Wednesday, January 23, 2013 3:38:22 PM UTC, Victor Bazarov wrote:
    > On 1/23/2013 10:24 AM, Bint wrote:


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


    > It does ! (logical NOT) twice.


    > > As in this statement


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


    > It's shorthand for


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


    Where "shorthand" is another word for obfuscation.

    --
    James
     
    James Kanze, Feb 24, 2013
    #8
    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,237
    Bill Johnson
    Jul 8, 2005
  2. Replies:
    3
    Views:
    486
    Ken Cox [Microsoft MVP]
    Oct 7, 2005
  3. Janet Collins
    Replies:
    0
    Views:
    1,154
    Janet Collins
    Jan 13, 2006
  4. Replies:
    1
    Views:
    849
    Juan T. Llibre
    Feb 8, 2006
  5. Alek Nazarian
    Replies:
    7
    Views:
    3,230
    Alek Nazarian
    Oct 22, 2003
Loading...

Share This Page