Are logical operators symmetric

Started by boral, September 23, 2012, 09:44:42 AM

Previous topic - Next topic

boral

Are the logical operators && and || symmetric
i.e. does x&&y and y&&x always give the same result?
Similarly, does x||y and y||x always give the same result?  :-\

Stefan Pendl

Why shouldn't they?

Have you an example where this is not the case?
---
Stefan

Proud member of the UltraDefrag Development Team

frankie

#2
&& and || are boolean operators, not logical operators...  ;)
They don't execute respectively bit by bit AND and OR logical operations.
Boolean operators consider the whole value (byte, word, doubleword, etc) true if they are not zero.
Ie if (a), is equivalento to if (a!=0).
So the code:
if (a && b)
is equivalent to
if ( (a!=0) && (b!=0) )
A boolean operation gives only two results: 1=TRUE, 0=FALSE
If you want the bit by bit logical operation AND or OR you must use '|' for OR and '&' for AND.
In the following code:
if (a & b)
if a=7 and b=3 the logical result is 3, the boolean result is TRUE because 3!=0.
I hope to have been clear....

Oh I forgot: yes, logical and boolean operators are simmetric.
Moreover the boolean operators are always optimized, so if the first term of an AND operation is false the second one is not evaluated (the reverse for boolean OR). So if the second term of your comparison is a function it is not called.
Ie

if (a && getvalue(b))
If a is false (a=0) the function getvalue is never called.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

boral

#3
Thanks to both of you and frankie thanks for your hard work....you have made a lot of things clear to me...thank u very much.