|
Shameel wrote: And in C# all logical operators always short-circuit.
You said all logical operators always short-circuit and that's not true. & and | are logical operators and they don't short-circuit. I never said & can't be a bitwise and but in this case it is a logical and .
|
|
|
|
|
I am tired of explaining to you, & is NOT a logical operator, it is a bitwise operator.
|
|
|
|
|
So, in this case - if (true & false) - & is not a logical AND?
|
|
|
|
|
It is your mistake that you are using & like a logical operator. It is NOT supposed to be used as a logical operator, we have && for that purpose.
|
|
|
|
|
Again, you're wrong, they are both logical operators in that case but one is short-circuited and the other is not!
You have the same think in VB with the And , AndAlso , Or , OrElse operators, they are all handy in different situations.
|
|
|
|
|
Please read the msdn links that you posted, VB is different and C# is different. VB has two logical operators for AND operation, And does not short-circuit and AndAlso short-circuits. This is a design decision for backward compatibility with VB6. But C# was designed from the ground-up, so the designers were not constrained by the need to be backward compatible, they had more freedom which made them design && to always short-circuit.
Read this link which CLEARLY states that & and && are different:
http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]
If you're still not convinced, then good luck to you, this is my last reply to this post.
|
|
|
|
|
What you don't seem to realise is that I'm not talking about the operators names but what they do.
From MSDN[^]:
& - logical AND
| - logical OR
&& - conditional AND
|| - conditional OR
From MSDN[^]:
The operation
x && y
corresponds to the operation
x & y
except that if x is false, y is not evaluated (because the result of the AND operation is false no matter what the value of y may be). This is known as "short-circuit" evaluation.
Which means I was right in my first post to say there was no difference, in that if condition, to use one or the other, the program still runs with no exceptions and as expected.
Your only argument so far is that one is called logical and the other bitwise which doesn't change the fact that I was right; call them what you want.
|
|
|
|
|
This is true only in case where the operands of & are both bool , but unlike && which takes only bool operands, & can also take int as operands which can lead to unexpected behavior.
|
|
|
|
|
And what's the case on the line of code in my first post?
|
|
|
|
|
Your case is an exception, don't treat it like a rule.
|
|
|
|
|
So are you admitting you were wrong?
I may or may not be responsible for my own actions
|
|
|
|
|
'AND' in VB is not equivalent to '&' in C#, but equivalent '&&'. this is applicable for OR also...
Only difference is 'AND' is not short circuited where '&&' is short circuited. To achieve short circuited and VB require explicit usage of 'AND ALSO'
|
|
|
|
|
But nobody here can understand why I continue to suggest we move from VB to C#.
Of course we want the default behavior to be as inefficient as possible!
|
|
|
|
|
In your example it's a waste of CPU cycles not to short circuit using &&. The exception is if you have somehow overloaded the & operator, or have logic in your Text property that needs to be evaluated every time.
|
|
|
|
|
http://msdn.microsoft.com/en-us/library/aa691306%28v=vs.71%29.aspx[^]
This (from Microsoft) includes & and | in the logical operators.
And semantics aside, they can and sometimes should be used as such in C# - for those cases where you want a logical operator without short-circuiting (i.e. one of the operands is a method call with side-effects - which would be a whole nother type of questionable practice, but the language allows it and it has is uses). So in that sense the OP is correct, & and && do the same thing except && short-circuits.
Of course, they don't do exactly the same thing, because & can also be used on non-boolean types as a bitwise operator. So (4 & 5) is meaningful, whereas 4 && 5 is not.
|
|
|
|
|
|
Fabio V Silva wrote: If you're working as C# developer I think you should RTFM.
No need to be impolite.
|
|
|
|
|
Fabio V Silva wrote: If you're working as C# developer I think you should RTFM.
You, sir, are a disgrace. Not only are you inaccurate in your assertions, you display the two traits I detest most about *some* people who work in this profession - a lack of respect for those around you, and a blinkered solipsism bordering on egomania.
Wise the f**k up son. Nobody's impressed.
|
|
|
|
|
Fabio V Silva wrote: They do the same except for the fact that && is short-circuited.
Except they don't do they? For boolean operands they do the same thing, (ignoring, as you said, the short circuiting which is a very good reason to use && ).
Take this example:
Console.WriteLine( 42 && 3);
Syntax error. Why? Because it is a Logical Operator which takes booleans as its operands.
Now take:
Console.WriteLine( 42 & 3)</pre>
Answer 2: Why? Because it is a Bitwise Logical Operator (though most people drop the "Logical") ANDing on the bits:
101010 AND
000011
------
000010
= 2.
The bitwise <code>|</code> and <code>&</code> operators are best left for work at the bit-level (such as bit-masking), whereas the the C# logical operators are better suited to boolean comparisons, like in the OP.
I wouldn't have voted you down BTW, but I would have pointed it out.
<div class="signature"><small><a href="http://tinyurl.com/3yl74on">Sort of a cross between Lawrence of Arabia and Dilbert.</a>[<a href="http://tinyurl.com/3yl74on" target="_blank" title="New Window">^</a>]<br>-Or-<br><a href="http://tinyurl.com/36qcqfr">A Dead ringer for Kate Winslett</a>[<a href="http://tinyurl.com/36qcqfr" target="_blank" title="New Window">^</a>]</small>
</div>
|
|
|
|
|
Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM.
|
|
|
|
|
You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...
|
|
|
|
|
Fabio V Silva wrote: You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...
I confirm before posting anything, check my reply.
|
|
|
|
|
As I have stated in my reply to Fabio, & and && are for different purpose. & is a bitwise operator and && is a logical operator, they may produce same output in certain situations, but it does not mean that you can use them interchangeably. In C#, logical operators always short-circuit.
See this link for an example:
http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]
|
|
|
|
|
The && operator should be used instead of the & operator for clarity's sake because its the standard way of doing things. Also the && operator gives you some type safety that the & operator does not when dealing with LOGICAL ANDS: (4 && 5) will not compile but (4 & 5) will. If you are intending to perform a LOGICAL AND operation you could end up performing a LOGICAL BITWISE AND operation instead by mistake.
But in any case, the reason you are giving:
"(...)In C#, logical operators always short-circuit(...)
is so wrong I dont even know where to begin.
The operator & is an overloadable operator. Integral and boolean types have their own predefined & binary operators: &(int, int) computes the logical bitwise AND of its operands while &(bool, bool) computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
This is all straight from the C# specifications:http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^]
So bool & bool is NOT A BITWISE OPERATION at all. Its a normal LOGICAL AND OPERATION where both terms are always evaluated contrary to bool && bool where the second term is evaluated if and only if the first term is true.
modified on Thursday, May 19, 2011 5:55 AM
|
|
|
|
|
Ha, that's what they taught me as well. It's utter nonsense off course since both '&' and '|' would be completely useless if that was the only difference.
It's just that it's a good habit to always use '&&' and '||' unless you need '&' or '|' specifically, even in cases where the result is the same.
|
|
|
|