2

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?

8
  • These operators are pretty much the same in all languages: en.wikipedia.org/wiki/Bitwise_operation Commented Jan 8, 2012 at 7:20
  • & and | have a double meaning from what I've found in that they can be bitwise operators (depending on their usage) or conditional operators (similar to && and ||) where they mean and and or where as && is equivalent to 'andalso' and || is 'orelse'. This article has a good explanation. Basically they are used to determine how your conditions are evaluated. Commented Jan 8, 2012 at 7:22
  • C# operators: msdn.microsoft.com/en-us/library/6a71f45d(v=VS.100).aspx Commented Jan 8, 2012 at 7:25
  • @M.Babcock: That's because VB.NET is backwards compatible with VB 6, which only had & and |, and thus used them as both bitwise and conditional operators. They don't really have a double meaning though; they're still performing bitwise operations. Commented Jan 8, 2012 at 7:25
  • 2
    @Int3ὰ: Because good answers do not make good questions? That's why there are separate vote counts for questions and answers. Commented Jan 8, 2012 at 7:26

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

18 Comments

why do we need eager boolean logic operators in C#? Never seen these used in C#, and could lead to subtle defects.
I don't understand, with the && and || operators, I want a bool returned, not another object...
+1 for pointing out one of the first things I learned when porting VB.NET/VB6 code to C#
What do the && and || operators have to do with anything but booleans?
@CodeInChaos: Alternatively, you could say that nullable bool just means "if either operand is null then the result is null". In that case if A() is null then clearly we don't need to evaluate B() -- we know that the result will be null. But now what if A() is false? Now we need to evaluate B() to know if it is null or not even when A() was false, which seems like it is going to cause problems when the code author assumed that B() would only be evaluated if A() was true. In short, it is a big freakin' mess and the best thing to do is avoid it entirely by not having the feature at all.
|
0

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)

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.

2 Comments

As Eric Lippert mentioned in his answer, sometimes && shouldn't return bool. For example, trinary logic as found in a database "Yes/No" column: DBFalse && x could return false, but DBTrue && DBNull must return DBNull.
Or in situations where you use unsafe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.