linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Using operator ! twice
       [not found] <1b2aacd80706291734y55b6e654i956767ae2a6f5e46@mail.gmail.com>
@ 2007-06-30  0:36 ` Luong Ngo
  2007-06-30  3:24   ` Pedro de Medeiros
  2007-07-01  1:08   ` Glynn Clements
  0 siblings, 2 replies; 3+ messages in thread
From: Luong Ngo @ 2007-06-30  0:36 UTC (permalink / raw)
  To: linux-c-programming

Hello,

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? this seems to be the same
for me if we do this in math:  -(-( -8)) = -8;

Any explanation would be appreciated.

Thanks,
-L

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using operator ! twice
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Pedro de Medeiros @ 2007-06-30  3:24 UTC (permalink / raw)
  To: Luong Ngo; +Cc: linux-c-programming

On 6/29/07, Luong Ngo <luong.ngo@gmail.com> wrote:
> Hello,
>
> 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? this seems to be the same
> for me if we do this in math:  -(-( -8)) = -8;


The reason for using double !s is to evaluate something as a
boolean value. I don't know why use it alone in "if ( !! num )",
but consider this code instead:

char *options[] = { "off" , "on" };

printf("Option is %s.\n", options[!!num]);

Since !!num always evaluates to 0 or 1, you don't have to
test it.


Cheers,
Pedro.

-- 
Pedro de Medeiros - Ciência da Computação - Universidade de Brasília
Home Page: http://www.nonseq.net - Linux User No.: 234250
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using operator ! twice
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2007-07-01  1:08 UTC (permalink / raw)
  To: Luong Ngo; +Cc: linux-c-programming


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>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-07-01  1:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [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 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).