and & operators?

edit: My main question now is why these two operators need to be overloaded to use the && and || operators. Wouldn't the short-circuit operators take the true or false values of the objects and compare those? Where in the use of the && and || operators is | and & used?

I'm reading C#, the complete reference, and I'm quite confused about the | and & operators. I'm used to them being bitwise operators that compare the bits of two integers, but they're explained as the original logical operators, to which && and || are the short-circuit versions that stop testing values when the statement is definitely going to be a certain value.

Does this mean that the two operators have multiple uses, or that C# does do some behind the scenes type casting?

Also, when using the && and || operators on a class, why do | and & have to be overloaded? Why can't just the true and false values for a class be overloaded?


The | and & operators are bitwise operations on integers and eager logic operators on Booleans. The || and && operators are lazy logic operators on Booleans.

Also, when using the && and || operators on a class, why do | and & have to be overloaded? Why can't just the true and false values for a class be overloaded?

You have a class Foo with an overloaded true and false operator. You wish the && operator on Foo to take two Foos and return a third. Explain how you plan to do so with only the true and false operators.

why do we need eager Boolean logic operators in C#?

In case you want to evaluate the side effects of two Boolean-returning values and apply a logical operation to them.

I don't understand, with the && and || operators, I want a bool returned, not another type.

Then you don't need to overload anything. Just write an implicit conversion operator from Foo to bool and you can use |, ||, & and && to your heart's content.

Just because you don't want to have a && or || operator that returns something other than bool doesn't mean that no one does. Suppose instead of Foo you wish to write a three valued logic operator where the value can be True, False or Neither. You could define & and | and && and || operators on True, False and Neither. Clearly you would not want the operators to return bool; you want them to return True, False or Neither.

What I don't understand is what the | and & operators have to do with the && and || operators.

They're exactly the same operators . The only difference is that the lazy ones don't evaluate the second operand if doing so is unnecessary.

Why must | and & be overloaded to use || and && though?

See the previous question and answer.

The | and & operators take two operands.

Yes, they do. So do the && and || operators.


I didn't expect to be able to find the answer online or else I wouldn't have asked here, but I did manage to, and here's what I have:

From http://msdn.microsoft.com/en-us/library/aa691312(v=vs.71).aspx it says: The operation x && y is evaluated as T.false(x) ? x : T.&(x, y) The operation x && y is evaluated as T.false(x) ? x : T.&(x, y)

In other words, it evaluates the true or false of x and if x is false, returns x, otherwise it returns x & y . This would explain why the two | and & operators need to be overloaded for && and ||, but it doesn't explain why it's not evaluated as:

T.false(x) ? x : T.false(y) ? y : x

If someone can explain why y and x are compared, I would appreciate it.

链接地址: http://www.djcxy.com/p/9890.html

上一篇: PHP赋值运算符=&

下一篇: 和&运营商?