![]() |
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 |
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 |
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 . |
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. |
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 |
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 |
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/> |
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.