admin管理员组

文章数量:1530842

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

Possible Duplicate:

What is the diffference between the | and || or operators?

逻辑与与或:

(x & y)

(x | y)

有条件的和或:

(x && y)

(x || y)

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

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

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()。

好啊。那懒惰的评价呢?你要提这个吗?因为如果你这样做,我可以删除我最不受欢迎的答案。

惰性评估是逻辑操作方式的一个子集。应该提及,但与问题无关。

@约翰·韦尔登:问题是操作者之间的区别。懒惰当然是其中之一。

@"短路"的概念特定于逻辑操作。它实际上不适用于按位运算,除非在某些情况下。

@罗伯特,@0xA3——我收回了我在这里的大部分观点和论点……具体见我的答案。

@罗伯特·哈维:我下班的时候把这个答案扔了……现在就回去吧。我已根据您的建议添加了一个编辑:)

请注意,逻辑vs条件是微软对它们的命名定义msdn.microsoft/en-us/library/6a71f45d%28v=vs.80%29.aspx

回答很草率,因为它不能区分布尔操作数和数值操作数(足够)。在C_中,布尔值上没有位运算符。

@亨克:看看我要回答的问题……未指定数据类型。这是一个一般的概念答案,而不是一个挑剔的细节答案。

当这个答案被用于另一个问题"证明"&是一个位布尔运算符时,我想到这里。没有一般的答案,你的开场白完全错了。

@亨克:所以你投票否决了这个答案,因为它不适用于一个不该回答的问题?o.o我建议你重新阅读这个答案实际适用的问题。我的第一句话没有错,它是在澄清最初问题中的误解。

答案是延续了&操作符的概念,其中c实际上有3个。这个问题也确实很模糊。但在问题上,这是允许的。

当微软明确地将它们称为逻辑和条件运算符时,第一句话会令人困惑。

@马尔拉兹:谢谢你的反馈…现在怎么样?

看起来不错,不过在第二段中,您仍然将"条件"和"逻辑"称为"逻辑"。我更习惯按位与按逻辑,但既然微软有自己的术语,至少应该承认。否则像我这样的人很容易困惑!

(x && y)

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

(x & y)

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

我相信这是完全错误的?你能举一下吗?(我同意第一个断言,但不同意第二个断言)

这只是故事的一半,可以说是不太重要的一半。

当然,但还有很多事情要做,不是吗?假设x和y不是布尔值?没有提到和&的位性质,这个答案就缺少一些东西。

这篇文章的赞成者能解释一下吗?这似乎是大错特错。

@约翰,这是绝对正确的。在布尔值上操作时,唯一的区别是"懒惰"。实际上,无论是在布尔型还是更大的整型上操作,操作都没有区别。它在两个位上执行一个位,并返回一个位。

@约翰,这是事实。根据第7.10.3节布尔逻辑运算符中的c规范:"如果x或y为真,x_y的结果为真。否则,结果为假。"另请参见第7.11节条件逻辑运算符:"操作x y对应于操作x y,除非仅当x为假时才计算y。"。

@约翰:这是参考资料。steve-fair-dev.blogspot/2007/02/…

是的,但问题是关于一般的运算符,而不是它们应用于一种类型(bool)

@罗伯特,我承认这与布尔人有关。你肯定同意这个问题比这更广泛吗?

@约翰,对于第二个,无论类型如何,都将始终对y进行评估。

无聊的。我想我会去回答其他一些问题。

我完全同意短路。当比较位运算符和逻辑运算符时,这是不相关的。

@约翰不仅是位(顺便说一下,这个问题没有提到)。它也作为"布尔逻辑运算符"重载。

@迈克尔;我明确地说,我同意双重运算符是短路运算符这一事实。我真正关心的是,如果您比较逻辑运算符和位运算符(在所有类型上,而不是子集上),那么短路评估是不相关的。

@约翰:我很确定短路是人们选择条件运算符而不是位运算符的主要原因。

@不,这可能是所有分歧的原因。这对我来说并不是一个重要的原因。也许对某些人来说是这样。风险在于,盲目选择一个位比较,因为它不会短路,如果没有意识到它实际上是一个位操作,可能会导致混淆的结果。

@约翰:事实上,我确实觉得这篇文章很欠缺,只回答了一半的问题。我对被接受的答案也有同样的感觉。

@阿农,@michael,@robert,@matthew;在阅读了规范之后更新了我的答案。

@约翰对不起,我把它理解为"我不同意第二个断言",而不是"懒惰的评估不是重点"。我同意使用&和不会用于这些类型的"确保评估所有条件"场景,但根据规范,这是完全合法和可接受的。

@迈克尔;说得通…我的信息有误导性。@罗伯特,看来这样做是为了不让我取消我的否决权…我很抱歉。

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

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

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

根据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.

&、|和^过载。对于两个布尔值,它们只是一个热切的(无条件的)逻辑运算符(ECMA-364&167;14.10.3)

@马修,这个问题被提出的范围比布尔人更广泛。

实际上我不同意。从第二个例子可以清楚地看出,x和y是布尔值。位运算符永远不会进入问题。

@马修同意,可以这样解释。

本文标签: 条件逻辑区别两个语言