C# 中逻辑 AND、OR 和条件 AND、OR 有什么区别?

发布于 2024-09-07 13:39:27 字数 394 浏览 4 评论 0原文

可能的重复:
| 之间有什么区别和 ||还是运算符?

逻辑AND 和OR:

(x & y)
(x | y)

条件AND 和OR:

(x && y)
(x || y)

到目前为止我只知道条件操作数。我知道它的作用以及如何在 if 语句中应用它。但是逻辑操作数的目的是什么?

Possible Duplicate:
What is the diffference between the | and || or operators?

Logical AND and OR:

(x & y)
(x | y)

Conditional AND and OR:

(x && y)
(x || y)

I've only known about conditional operands up to this point. I know what it does and how to apply it in if-statements. But what is the purpose of logical operands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-09-14 13:39:27

我更喜欢将其视为“按位与条件”而不是“逻辑与条件”,因为“逻辑”的一般概念适用于这两种情况。

x & y    // bitwise AND, 0101 & 0011 = 0001
x | y    // bitwise OR,  0101 | 0011 = 0111

x && y   // true if both x and y are true
x || y   // true if either x or y are true

编辑

根据大众的要求,我还应该提到,对论点的评估是不同的。在条件版本中,如果整个操作的结果可以由第一个参数确定,则不计算第二个参数。这称为短路评估。位运算必须评估两边才能计算最终值。

例如:

x.foo() && y.bar()

如果 x.foo() 计算结果为 true,则仅调用 y.bar()。相反,

x.foo() || y.bar()

如果 x.foo() 计算结果为 false,则仅调用 y.bar()

I prefer to think of it as "bitwise vs. conditional" rather than "logical vs conditional" since the general concept of "logical" applies in both cases.

x & y    // bitwise AND, 0101 & 0011 = 0001
x | y    // bitwise OR,  0101 | 0011 = 0111

x && y   // true if both x and y are true
x || y   // true if either x or y are true

Edit

By popular demand, I should also mention that the arguments are evaluated differently. In the conditional version, if the result of the entire operation can be determined by the first argument, the second argument is not evaluated. This is called short-circuit evaluation. Bitwise operations have to evaluate both sides in order to compute the final value.

For example:

x.foo() && y.bar()

This will only call y.bar() if x.foo() evaluates to true. Conversely,

x.foo() || y.bar()

will only call y.bar() if x.foo() evaluates to false.

久光 2024-09-14 13:39:27
(x && y) 

很懒。如果 x 为真,它只会评估 y。

(x & y)

并不懒惰。 y 将始终被评估。

(x && y) 

is lazy. It will only evaluate y if x is true.

(x & y)

is not lazy. y will always be evaluated.

踏雪无痕 2024-09-14 13:39:27

更新答案 - 我的原始答案具有误导性且不完整。

首先,我应该为我对这个问题的大部分评论和回答道歉。

阅读规范后,按位运算符和条件运算符之间的区别就不太清楚了。

根据 ECMA-334 的14.10部分:

&、^ 和 |运算符被称为
逻辑运算符。

对于整数运算:

1 与运算符按位计算
两个操作数的逻辑与,|
运算符计算按位逻辑
两个操作数的或,以及 ^
运算符计算按位逻辑
两个操作数的异或。 2 否
这些可能会发生溢出
操作。

根据14.11:部分

&&和 ||运算符被称为
条件逻辑运算符。 2 他们
也称为“短路”
逻辑运算符。

14.11.1

1 当 && 的操作数为或||属于
类型 bool,或者当操作数为
没有定义适用的类型
运算符 &或运算符 |,但执行
定义到 bool 的隐式转换,
操作处理如下:
2 运算x && y 被评估为
x ? y:假。 3 换句话说,x 是
首先评估并转换为类型
布尔。 4 那么,如果 x 为真,则 y 为
求值并转换为 bool 类型,
这成为以下结果
手术。 5 否则,结果为
该操作是错误的。 6 的
操作 x || y 被评估为 x ?
正确:是的。 7 换句话说,x 是第一个
计算并转换为 bool 类型。
8 那么,如果 x 为真,则结果为
操作正确。 9 否则,y
被评估并转换为类型
bool,这成为以下结果
操作。

14.11.2

1 当 && 的操作数为或||属于
声明适用的类型
用户定义的运算符 &或运算符 |,
以下两项必须为真,
其中 T 是类型,其中
声明所选运算符:2
返回类型和每个的类型
所选算子的参数
必须是 T。 3 换句话说,
运算符必须计算逻辑 AND
或两个操作数的逻辑或
类型 T,并且必须返回结果
类型 T。4 T 必须包含声明
运算符 true 和运算符 false。
第 2 段 1 编译时错误
如果出现这些要求之一
不满意。 2 否则,&&
或||操作评估为
组合用户定义的运算符
true 或运算符 false 与
选定的用户定义运算符:3
操作 x && y 被评估为
T.假(x) ? x : T.&(x, y),其中
T.false(x) 是对
T 中声明了运算符 false,并且
T.&(x, y) 是对
选定的运算符 &。 4 换句话说,
首先对 x 进行求值和运算符
false 在结果上被调用
确定 x 是否绝对为假。 5
那么,如果 x 肯定为假,则
运算的结果是值
先前计算的 x。 6
否则,计算 y,并且
选定的操作员 &被调用于
先前为 x 计算的值和
为 y 计算产生的值
操作的结果。 7 的
操作 x || y 被评估为
T.true(x) ? x : T.|(x, y),其中
T.true(x) 是对
T 中声明了运算符 true,并且
T.|(x, y) 是对
选定的运算符|。 8 换句话说,
x 首先被求值且运算符为 true
对结果调用以确定
如果 x 绝对为真。 9 那么,如果 x
肯定是真的,结果是
操作就是之前的值
计算 x。 10 否则,y 是
评估,并选择运算符 |
在之前的值上调用
计算 x 和计算值
y 产生的结果
手术。第 3 段 1 在任一
这些操作,给出的表达式
by x 只计算一次,并且
y 给出的表达式要么不是
评估或仅评估一次。
第 4 段 1 类型示例
实现运算符 true 和
运算符 false,请参阅第 18.4.2 节。

Updated Answer - my original was misleading and incomplete.

First I should apologize for much of my comments and responses to this question.

After reading the spec, the distinction between bitwise and conditional operators is much less clear cut.

According to section 14.10 of ECMA-334:

The &, ^, and | operators are called
the logical operators.

for integer operations:

1 The & operator computes the bitwise
logical AND of the two operands, the |
operator computes the bitwise logical
OR of the two operands, and the ^
operator computes the bitwise logical
exclusive OR of the two operands. 2 No
overflows are possible from these
operations.

According to section 14.11:

The && and || operators are called the
conditional logical operators. 2 They
are also called the "short-circuiting"
logical operators.

14.11.1

1 When the operands of && or || are of
type bool, or when the operands are of
types that do not define an applicable
operator & or operator |, but do
define implicit conversions to bool,
the operation is processed as follows:
2 The operation x && y is evaluated as
x ? y : false. 3 In other words, x is
first evaluated and converted to type
bool. 4 Then, if x is true, y is
evaluated and converted to type bool,
and this becomes the result of the
operation. 5 Otherwise, the result of
the operation is false. 6 The
operation x || y is evaluated as x ?
true : y. 7 In other words, x is first
evaluated and converted to type bool.
8 Then, if x is true, the result of
the operation is true. 9 Otherwise, y
is evaluated and converted to type
bool, and this becomes the result of
the operation.

14.11.2

1 When the operands of && or || are of
types that declare an applicable
user-defined operator & or operator |,
both of the following must be true,
where T is the type in which the
selected operator is declared: 2 The
return type and the type of each
parameter of the selected operator
must be T. 3 In other words, the
operator must compute the logical AND
or the logical OR of two operands of
type T, and must return a result of
type T. 4 T must contain declarations
of operator true and operator false.
Paragraph 2 1 A compile-time error
occurs if either of these requirements
is not satisfied. 2 Otherwise, the &&
or || operation is evaluated by
combining the user-defined operator
true or operator false with the
selected user-defined operator: 3 The
operation x && y is evaluated as
T.false(x) ? x : T.&(x, y), where
T.false(x) is an invocation of the
operator false declared in T, and
T.&(x, y) is an invocation of the
selected operator &. 4 In other words,
x is first evaluated and operator
false is invoked on the result to
determine if x is definitely false. 5
Then, if x is definitely false, the
result of the operation is the value
previously computed for x. 6
Otherwise, y is evaluated, and the
selected operator & is invoked on the
value previously computed for x and
the value computed for y to produce
the result of the operation. 7 The
operation x || y is evaluated as
T.true(x) ? x : T.|(x, y), where
T.true(x) is an invocation of the
operator true declared in T, and
T.|(x, y) is an invocation of the
selected operator |. 8 In other words,
x is first evaluated and operator true
is invoked on the result to determine
if x is definitely true. 9 Then, if x
is definitely true, the result of the
operation is the value previously
computed for x. 10 Otherwise, y is
evaluated, and the selected operator |
is invoked on the value previously
computed for x and the value computed
for y to produce the result of the
operation. Paragraph 3 1 In either of
these operations, the expression given
by x is only evaluated once, and the
expression given by y is either not
evaluated or evaluated exactly once.
Paragraph 4 1 For an example of a type
that implements operator true and
operator false, see §18.4.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文