linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: Luong Ngo <luong.ngo@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Using operator ! twice
Date: Sun, 1 Jul 2007 02:08:50 +0100	[thread overview]
Message-ID: <18054.65186.556018.76413@cerise.gclements.plus.com> (raw)
In-Reply-To: <1b2aacd80706291736s18563893q83a19d0ae5662514@mail.gmail.com>


Luong Ngo wrote:

> I came across several times codes that use ! operator on an integer
> variable. I am curious what is the purpose of using it twice.
> For example:
> 
> int flag;
>                if( !! flag) {
>                    //do something
>                }
> 
> 
> The first ! operator will make !flag to be true if flag is 0, and if
> we not again it become false, which is just exactly the same if we
> just left it as
>    if(flag), since 0 is the same as false. Similar logic for non-0
> value of flag. Then why do we need to use ! operator twice to get back
> the same value as if we don't use at all?

The !! idiom converts anything which can be used as a boolean to an
integer which is either zero or one.

It isn't necessary in a context where any boolean value is acceptable
(e.g. the test of an if, while or do-while statement, or an operand to
the && and || operators), but is useful if you need a value which must
be either zero or one.

> this seems to be the same
> for me if we do this in math:  -(-( -8)) = -8;

However: !(-8) = 0, and !!(-8) = 1.

-- 
Glynn Clements <glynn@gclements.plus.com>

      parent reply	other threads:[~2007-07-01  1:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1b2aacd80706291734y55b6e654i956767ae2a6f5e46@mail.gmail.com>
2007-06-30  0:36 ` Using operator ! twice Luong Ngo
2007-06-30  3:24   ` Pedro de Medeiros
2007-07-01  1:08   ` Glynn Clements [this message]

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=18054.65186.556018.76413@cerise.gclements.plus.com \
    --to=glynn@gclements.plus.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=luong.ngo@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).