Logic
-
I don't know who voted you down, but there is lot of difference between using
&
and&&
in an if statement.&
is a bitwise operation and&&
is the logical operator.Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM. :sigh:
-
So, are you saying you can't use
&
in that case?! They do the same except for the fact that && is short-circuited. -
I don't know which language you are talking about, but in C#, they have different meaning, bitwise operators and logical operators are not the same. And in C# all logical operators always short-circuit.
-
Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM. :sigh:
You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...
-
Read my comments again, I said that the LOGICAL operators (&&, ||) always short-circuit, but bitwise operators never short-circuit. The links you posted say exactly the same. See this example for the difference between
&
and&&
: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^] -
You would think people would at least read it to confirm what they are saying if they are trying to disprove someone...
-
And before asking me to RTFM, you should go to a nursery that teaches the rudiments of programming. BITWISE operators and LOGICAL operators have totally different meanings even though they may produce the same output in certain situations: Read this link again: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]
-
Read my comments again, I said that the LOGICAL operators (&&, ||) always short-circuit, but bitwise operators never short-circuit. The links you posted say exactly the same. See this example for the difference between
&
and&&
: http://blog.dmbcllc.com/2009/03/16/vs-and-vs-whats-the-difference/[^]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 abitwise and
but in this case it is alogical and
. -
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 abitwise and
but in this case it is alogical and
. -
So, in this case -
if (true & false)
-&
is not a logical AND? -
So, in this case -
if (true & false)
-&
is not a logical AND? -
Really? And I always thought that '&&' was simply a short-circuited '&'. I must go RTFM. :sigh:
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/[^] -
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. -
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 andAndAlso
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. -
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 andAndAlso
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 operationx & 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. -
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 operationx & 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 bothbool
, but unlike&&
which takes onlybool
operands,&
can also takeint
as operands which can lead to unexpected behavior.And what's the case on the line of code in my first post?
-
This should really mess with your noggin:
bool isTrue = true || true && false; // True
bool isFalse = true | true && false; // False.Also, given your example, I'd prefer the double ampersand for 2 reasons: 1) for short-circuiting and 2) for standards' sake.
-
This should really mess with your noggin:
bool isTrue = true || true && false; // True
bool isFalse = true | true && false; // False.Also, given your example, I'd prefer the double ampersand for 2 reasons: 1) for short-circuiting and 2) for standards' sake.
I was just discussing the case that someone said that line was incorrect and downvoted my answer when in reality I just copied the OP's line and changed the = to == which is what was actually stopping it from working as expected. I prefer the double ampersand as well as a standard, the same way I always try to use short circuiting in VB which doesn't seem to be common practice, at least in online samples and articles.
-
And what's the case on the line of code in my first post?