# Re: What does !! do?

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

1. ### Victor BazarovGuest

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
--

Victor Bazarov, Jan 23, 2013

2. ### Ian CollinsGuest

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

3. ### Jorgen GrahnGuest

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
4. ### Öö TiibGuest

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
5. ### Tobias MÃ¼llerGuest

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
6. ### Tobias MÃ¼llerGuest

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
7. ### army1987Guest

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
8. ### James KanzeGuest

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