Logic
-
Jesus, why do people pretend to know the absolute truth about things without even bothering to do some minimal research. What you are stating is completely wrong. The
&
operator is an OVERLOADABLE operator. As such, it has predefined behaviours for integral types and boolean types.(int & int)
IS NOT THE SAME ASbool & bool
. The first performs a logical bitwise AND operation while the latter performs a LOGICAL AND operation. There is no bitwise operation at all if the operator is dealing with two booleans. It is exactly the same asbool && bool
except that both terms are evaluated no matter what the first expression evaluates to. If you are not convinced then please read the following MSDN C# reference link: http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx[^] or better yet: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx[^]Well f. me sideways and call me Dr Dream. You come and tell me that I don't know what I'm saying and immediately say what I said. Bitwise means EVERYTHING is evaluated and then anded ored noted xored and stuck through the mincer. Binary menas once the result is known it stops. I appologise if using technical terms confussed you but that's it.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
-
Again, what makes you think I don't know the difference between & and &&?! I never said they do the same thing and I do know that && is the most commonly used. That, however, does not make it wrong to use & between two boolean and it does not invalidate the fact that in that case it makes no difference. And if someone says it does then they should RTFM!
-
So if I have this straight, in your example:
if(UsernameTextBox.Text == "Manager" & PasswordTextBox.Text == "Maintenance")
because both arguments are boolean, the '&' is effectively acting just like a '&&' except for being trivially less efficient because it is always doing both of the string compares. I know in this case you were merely quoting previous post using '&', but even if the non-short-circuit behaviour would be useful sometime, I'd avoid it because it just looks wrong to me. We're in a mixed C++/C# environment here, and I have to be on the lookout for misused '&'s in the code as it is. Allowing for false positives is not in the cards here. That said, I think you got a raw deal.
-
And quite rightly so. While for booleans, & can work as a logical operator, in all other cases it is bitwise. For consistency, use a single operatopr to represent logical operators throughout, the C# designers (C really) chose && for this purpose. It may work, but its obfuscated, and should be rejected or corrected by any reasonable code review, regardless of any appeals you make to technical documentation.
-
And quite rightly so. While for booleans, & can work as a logical operator, in all other cases it is bitwise. For consistency, use a single operatopr to represent logical operators throughout, the C# designers (C really) chose && for this purpose. It may work, but its obfuscated, and should be rejected or corrected by any reasonable code review, regardless of any appeals you make to technical documentation.
Rob, I think the problem is that you are assuming human beings are rational/reasonable. Where I am currently, I have to fill out a form and get authorization to fix a simple memory leak. The code base is several million lines of C++, suffering from 20 years of technical debt. Since I already have a reputation for being "too critical about code quality" which causes my input to get knocked down a level or two, I have to bite my tongue a lot. It's a grand learning experience, but I'll be glad when I figure out what the lesson is!
-
Rob, I think the problem is that you are assuming human beings are rational/reasonable. Where I am currently, I have to fill out a form and get authorization to fix a simple memory leak. The code base is several million lines of C++, suffering from 20 years of technical debt. Since I already have a reputation for being "too critical about code quality" which causes my input to get knocked down a level or two, I have to bite my tongue a lot. It's a grand learning experience, but I'll be glad when I figure out what the lesson is!
The lesson : Never start a fight in the Hall of Shame :laugh:
-
Fabio V Silva wrote:
If you're working as C# developer I think you should RTFM.
No need to be impolite.
-
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. -
'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'
-
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. -
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.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.
-
The "answer" person's assumptions doesn't seem to match with what you have in your message. Using & is different than using && and the results could be different, depending on what you are comparing. Since both return either true of false, there will not be a difference in the result. (The only difference is how the result is achieved.) In C and C++, you can "AND items that are not boolean as: int i, j; i = 1; j = 2; if (i and j) --> result is false (bitwise AND: 1 & 2 yields 0 or false). if (i and j) --> result is true (logical AND: 1 && 2 yields non-zero or true). Hope this helps.
-
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.
Smokie, this is not 'Nam. This is bowling. There are rules. www.geticeberg.com http://melpadden.wordpress.com