Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: What does !! do? (http://www.velocityreviews.com/forums/t956841-re-what-does-do.html)

Victor Bazarov 01-23-2013 03:38 PM

Re: What does !! do?
 
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

Ian Collins 01-23-2013 10:25 PM

Re: What does !! do?
 
Scott Lurndal wrote:
> Victor Bazarov <v.bazarov@comcast.invalid> 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

Jorgen Grahn 01-23-2013 11:06 PM

Re: What does !! do?
 
On Wed, 2013-01-23, Ian Collins wrote:
> Scott Lurndal wrote:
>> Victor Bazarov <v.bazarov@comcast.invalid> 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 .

Öö Tiib 02-19-2013 11:32 PM

Re: What does !! do?
 
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.

Tobias Müller 02-20-2013 10:37 AM

Re: What does !! do?
 
Bint <bint@ign.com> 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 02-20-2013 10:42 AM

Re: What does !! do?
 
Tobias Müller <troplin@bluewin.ch> wrote:
> Bint <bint@ign.com> 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

army1987 02-20-2013 12:21 PM

Re: What does !! do?
 
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/>

James Kanze 02-24-2013 05:32 PM

Re: What does !! do?
 
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


All times are GMT. The time now is 02:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.