非零!= 1,对吧? [英] nonzero != 1, right?
问题描述
有人可以确认以下内容
布尔表达式,计算结果为true或false。在整数
上下文中,它分别变为非零或零。
因此表达式
x == y? 0:1
不能缩写为
x == y
谢谢,
-trent
Could someone please confirm the following
A boolean expression, evaluates to true or false. In an integer
context, this becomes non-zero or zero, respectively.
Therefore the expression
x == y ? 0 : 1
CANNOT be abbreviated to
x == y
Thanks,
-trent
推荐答案
文章< 20 ******************** *@harpo.marx> ;,
Trent Buck< NO ************ @ bigpond.com>写道:
In article <20*********************@harpo.marx>,
Trent Buck <NO************@bigpond.com> wrote:
有人可以确认以下
一个布尔表达式,计算结果为true或false。在整数
上下文中,它分别变为非零或零。
因此表达式
x == y? 0:1
不能缩写为
x == y
Could someone please confirm the following
A boolean expression, evaluates to true or false. In an integer
context, this becomes non-zero or zero, respectively.
Therefore the expression
x == y ? 0 : 1
CANNOT be abbreviated to
x == y
所有返回布尔结果的运算符都被定义为返回0或
1,所以你可以这样做,或者至少可以,如果你不介意反转
结果。
正确的缩写将是'x!= y'',因为你的第一个表达式
如果它们相等则返回0。
注意这只是适用于运算符而不适用于一般情况;
f''re示例,< ctype.h>中的isfoo函数或宏。被定义为
返回非零,不一定是一个,作为是值。一个简单的方法
将零或非零折叠为零或一个是
`!''运算符的两个应用程序(" !! val") 。
dave
-
Dave Vandervies dj ****** @ csclub.uwaterloo.ca
虽然想起来如果你不喜欢听起来很愚蠢你就不会'' t
谈论你不理解的事情。没关系......
- 大卫C. Ullrich在comp.lang.c烤一个巨魔。
All operators that return a boolean result are defined to return 0 or
1, so you can do this, or at least could if you didn''t mind inverting
the result.
The correct abbreviation would be to `x != y'', since your first expression
returns 0 if they''re equal.
Note that this only applies to operators and not to the general case;
f''rexample, the isfoo function-or-macros in <ctype.h> are defined to
return nonzero, not necessarily one, as a "yes" value. An easy way
to fold zero-or-nonzero to zero-or-one is two applications of the
`!'' operator ("!!val").
dave
--
Dave Vandervies dj******@csclub.uwaterloo.ca
although come to think of it if you didn''t enjoy sounding stupid you wouldn''t
be talking about things you don''t understand. never mind...
--David C. Ullrich roasts a troll in comp.lang.c++
Trent Buck< NO ** **********@bigpond.com>潦草地写道:
Trent Buck <NO************@bigpond.com> scribbled the following:
有人可以确认以下
一个布尔表达式,计算结果为true或false。在整数
上下文中,它分别变为非零或零。
因此表达式
x == y? 0:1
不能缩写为
x == y
Could someone please confirm the following A boolean expression, evaluates to true or false. In an integer
context, this becomes non-zero or zero, respectively. Therefore the expression
x == y ? 0 : 1 CANNOT be abbreviated to
x == y
将整数视为真值并且
反之亦然。 *任何*非零整数值表示真。当用作
真值时,例如在if(x)中。但是,布尔运算符如
为==,!=,<"等等* *总是*返回1而其他没有用于真。并且
0表示false。因此表达式(x == y)和((x == y)?1:0)是
同义词。 (你的原始表达中有0和1错误
加上我对优先级不太确定。)
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \
\ ------------------------------------------ --------------规则! -------- /
"''可以很容易地证明'''的意思''我看过一次这样的证明(我没有这样做)
明白了)我不能再记得''。
- 数学老师
There is a difference between treating an integer as a truth value and
vice versa. *Any* non-zero integer value means "true" when used as a
truth value, for example in "if (x)". However, boolean operators such
as "==", "!=", "<", etc. *always* return 1 and no other for "true" and
0 for "false". Therefore the expressions (x==y) and ((x==y)?1:0) are
synonymous. (You got the 0 and 1 wrong in your original expression
plus I''m not too sure about the precedence.)
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"''It can be easily shown that'' means ''I saw a proof of this once (which I didn''t
understand) which I can no longer remember''."
- A maths teacher
Quoth Dave Vandervies on or about 2004-11-11:
Quoth Dave Vandervies on or about 2004-11-11:
因此表达式
x == y? 0:1
不能缩写为
x == y
所有返回布尔结果的运算符都被定义为返回0或
1,所以你可以做这个,或者至少可以,如果你不介意反转结果。
Therefore the expression
x == y ? 0 : 1
CANNOT be abbreviated to
x == y
All operators that return a boolean result are defined to return 0 or
1, so you can do this, or at least could if you didn''t mind inverting
the result.
谢谢。哎呀,它已经过了我的睡觉时间。
将零或非零折叠到零或一的简单方法是`!''运算符的两个应用程序( " !! val")。
Thank you. And whoops, it''s past my bedtime.
An easy way to fold zero-or-nonzero to zero-or-one is two applications
of the `!'' operator ("!!val").
是的。我已经看到了这个分裂和分散的典型例子。
int
dru(int x,int y)
{
返回x / y + !!(x%y);
}
。 ......如果我还在醒着。
Yup. I''ve seen that in the canonical example of divide-and-round-up.
int
dru (int x, int y)
{
return x/y + !!(x%y);
}
....if I''m still awake.
这篇关于非零!= 1,对吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!