逻辑和条件与,或C之间的区别是什么?

What is the difference between logical and conditional AND, OR in C#?

本问题已经有最佳答案,请猛点这里访问。

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

逻辑与与或:

1
2
(x & y)
(x | y)

有条件的和或:

1
2
(x && y)
(x || y)

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


我更愿意把它看作是"位对条件"而不是"逻辑对条件",因为"逻辑"的一般概念适用于这两种情况。

1
2
3
4
5
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

编辑

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

例如:

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

只有当x.foo()的计算结果为true时,才会调用y.bar()。相反地,

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

只有当x.foo()的计算结果为false时,才会调用y.bar()


1
(x && y)

懒惰。只有当x为真时,它才会计算y。

1
(x & y)

不是懒惰。Y将始终被评估。


更新的答案-我的原稿误导和不完整。

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

在阅读了规范之后,位运算符和条件运算符之间的区别就不那么明显了。

根据ECMA-334第14.10节:

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

对于整数运算:

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.

根据第14.11节:

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

14.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.

141.1.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.