!!, 它是什么? [英] !!, what is it?

查看:56
本文介绍了!!, 它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在一些代码中偶然发现了这个:


n = !! x;


我已经运行它了''n''总是0或1.是''!!''是一个操作员(它是什么?

叫做?)是标准/便携式吗?


-

benkibbey ::: bjk AT arbornet DOT org

解决方案

" bjk当然 <使用-作者提供地址报头@ [127.1] GT;写在

消息新闻:20 ******************** @ cpe-66-1-217-9.fl.sprintbbd.net ...

你好,

我在一些代码中偶然发现了这个:

n = !! x;

我已经运行了它并且''n''总是0或1.是''!!''是一个运算符(它叫什么
?)并且它是标准的/便携式?




n = !! x;

应相当于:

n =!(!x) ;

甚至:

n =!(!(x));


如果x非零,则n应为1 ,如果x为零,则为0。


有了这个主意吗?


Alex


当然是bjk <使用-作者提供地址报头@ [127.1] GT;写在

消息新闻:20 ******************** @ cpe-66-1-217-9.fl.sprintbbd.net ...

你好,

我在一些代码中偶然发现了这个:

n = !! x;

我已经跑了,''''总是0或1.


是的,因为那总是由于! (逻辑不是)

运营商。

是''!!''运营商


不,!是一个运营商。它的结果总是为零或一。

它只是在上面的代码中被使用了两次。例如


!0 == 1

!1 == 0

!42 == 0;

!! 42 == 1 / *因为!42 == 0,然后!(!42)== 1

(因为!0 == 1)* /



(它的名字是什么?)


逻辑没有。

并且是标准/便携式吗?




是的。


int i = 42;

int j =!i; / *用0 * /

int k初始化''j''; = !!我; / *用1 * /

int m =!j初始化''k''; / *初始化''m''用1 * /


逻辑

非运算符的'双重应用''的典型原因是将非零值(在逻辑上将
总是计算为''true'')转换为特定的''true''值(在

中,这种情况为一(1))。需要这样做的原因取决于应用程序上的
。这是一个简单的人为例子:


#include< stdio.h>


/ *零值无效,任何非零值有效* /

void validate(int value)

{

static const char * msg [] = {" invalid"," valid"};

printf(" value%d is%s \ n",msg [ !! i]);

/ *注意!! 0总是正好0 * /

}


int main ()

{

验证(42);

返回0;

}


IOW,!42 == 0,!! 42 == 1,!! 42!= 42


如果有的话需要其他特定的真实价值,人们只需要对价值1执行更多算术(例如,添加

一些价值)。


-Mike


bjk当然是ha scritto:

你好,

我在一些代码中偶然发现了这个:

n = !! x;

我'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >


这意味着''不是x'',如果x为0则评估为0或如果x不为0则评估为1.


-

Devaraja(Xdevaraja87 ^ gmail ^ c0mX)

Linux注册用户#338167
http://counter.li.org


hello,

I''ve stumbled upon this in some code:

n = !!x;

I''ve ran it and ''n'' is always either 0 or 1. Is ''!!'' an operator (what''s it
called?) and is it standard/portable?

--
b e n k i b b e y ::: bjk AT arbornet DOT org

解决方案

"bjk of course" <Use-Author-Supplied-Address-Header@[127.1]> wrote in
message news:20********************@cpe-66-1-217-9.fl.sprintbbd.net...

hello,

I''ve stumbled upon this in some code:

n = !!x;

I''ve ran it and ''n'' is always either 0 or 1. Is ''!!'' an operator (what''s it called?) and is it standard/portable?



n = !!x;
should be equivalent to:
n = !(!x);
or even:
n = !(!(x));

And n should be 1 if x is nonzero, 0 if x is zero.

Got the idea?

Alex


"bjk of course" <Use-Author-Supplied-Address-Header@[127.1]> wrote in
message news:20********************@cpe-66-1-217-9.fl.sprintbbd.net...

hello,

I''ve stumbled upon this in some code:

n = !!x;

I''ve ran it and ''n'' is always either 0 or 1.
Yes, because that''s always the result of the ! ("logical not")
operator.
Is ''!!'' an operator
No, ! is an operator. It''s result is always either zero or one.
It is simply being used twice in the above code. E.g.

!0 == 1
!1 == 0
!42 == 0;
!!42 == 1 /* because !42 == 0, then !(!42) == 1
(because !0 == 1) */
etc.
(what''s it
called?)
"Logical not".
and is it standard/portable?



Yes.

int i = 42;
int j = !i; /* initializes ''j'' with 0 */
int k; = !!i; /* initializes ''k'' with 1 */
int m = !j; /* initializes ''m'' with 1 */

The typical reason for this ''double application'' of the logical
not operator is to transform a nonzero value (which logically
always evaluates to ''true'') into a specific ''true'' value (in
this case one (1). The reason for needing to do this depends
upon the application. Here''s a simple contrived example:

#include <stdio.h>

/* zero value is invalid, any nonzero value is valid */
void validate(int value)
{
static const char *msg[] = {"invalid", "valid"};
printf("value %d is %s\n", msg[!!i]);
/* note that !!0 always is exactly 0 */
}

int main()
{
validate(42);
return 0;
}

IOW, !42 == 0 , !!42 == 1 , !!42 != 42

If some other specific ''true'' value were needed, one would
simply perform more arithmetic upon the value one (e.g. add
some value).

-Mike


bjk of course ha scritto:

hello,

I''ve stumbled upon this in some code:

n = !!x;

I''ve ran it and ''n'' is always either 0 or 1. Is ''!!'' an operator (what''s it
called?) and is it standard/portable?



It means ''not not x'', evaluating to 0 if x is 0 or to 1 if x is not 0.

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org


这篇关于!!, 它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆