public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Helge Hafting <helge.hafting@aitel.hist.no>
To: Vadim Lobanov <vlobanov@speakeasy.net>
Cc: Nick Warne <nick@linicks.net>, linux-kernel@vger.kernel.org
Subject: Re: A pettiness question.
Date: Thu, 22 Sep 2005 09:10:09 +0200	[thread overview]
Message-ID: <433258D1.8080301@aitel.hist.no> (raw)
In-Reply-To: <Pine.LNX.4.58.0509211305250.24543@shell4.speakeasy.net>

Vadim Lobanov wrote:

>On Wed, 21 Sep 2005, Nick Warne wrote:
>
>  
>
>>>>This give a enum of {0,1}. If test is not 0, !!test will give 1,
>>>>otherwise 0.
>>>>
>>>>Am I right?
>>>>        
>>>>
>>>Yes.  I think of it as a "truth value" predicate (or operator).
>>>      
>>>
>>Interesting.  I thought maybe this way was trick, until later I experimented.
>>
>>My post here (as Bill Stokes):
>>
>>http://www.quakesrc.org/forums/viewtopic.php?t=5626
>>
>>So what is the reason to doing !!num as opposed to num ? 1:0 (which is more
>>readable I think, especially to a lesser experienced C coder).  Quicker to
>>type?
>>    
>>
>
>Some people also prefer the following form:
>	num != 0
>  
>
That one looks good for if-tests and such. But if you need
a 0 or 1 for adding to a counter, then

a += !!x;

looks much better than

a += (x != 0);

There is nothing special about !!.  Just learn to use it, it
is easy to understand/read, and saves typing.

Perhaps a "C-contructs FAQ" might be useful, for the benefit
of newbies and beginners.  It could document things like
the use of !!, common kernel macro tricks, when not to
use zero initializers, and so on.

Helge Hafting

  reply	other threads:[~2005-09-22  7:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-21 19:46 A pettiness question Nick Warne
2005-09-21 20:05 ` Vadim Lobanov
2005-09-22  7:10   ` Helge Hafting [this message]
2005-09-22 12:21     ` Steven Rostedt
2005-09-22 14:40       ` linux-os (Dick Johnson)
2005-09-22  2:43 ` Fawad Lateef
     [not found] <4PiLw-2yn-25@gated-at.bofh.it>
     [not found] ` <4Pj4M-3as-1@gated-at.bofh.it>
     [not found]   ` <4PtnM-1oW-55@gated-at.bofh.it>
     [not found]     ` <4Pynh-fp-1@gated-at.bofh.it>
2005-09-25 14:38       ` Bodo Eggert
  -- strict thread matches above, loose matches on Subject: below --
2005-09-21  7:49 liyu
2005-09-21  8:00 ` Ustyugov Roman
2005-09-21  8:57   ` Eyal Lebedinsky
2005-09-21  9:01   ` Fawad Lateef
2005-09-21  9:30     ` Andrea Arcangeli
2005-09-23  2:36       ` Vadim Lobanov
2005-09-21 15:08   ` Randy.Dunlap
2005-09-21 19:11 ` Bill Davidsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=433258D1.8080301@aitel.hist.no \
    --to=helge.hafting@aitel.hist.no \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nick@linicks.net \
    --cc=vlobanov@speakeasy.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox