NO

Author Topic: Are logical operators symmetric  (Read 3317 times)

boral

  • Guest
Are logical operators symmetric
« on: September 23, 2012, 09:44:42 AM »
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?  :-\

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Are logical operators symmetric
« Reply #1 on: September 23, 2012, 11:37:48 AM »
Why shouldn't they?

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

Proud member of the UltraDefrag Development Team

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Are logical operators symmetric
« Reply #2 on: September 23, 2012, 05:24:19 PM »
&& 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
Code: [Select]
if (a), is equivalento to
Code: [Select]
if (a!=0).
So the code:
Code: [Select]
if (a && b)is equivalent to
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
if (a && getvalue(b))If a is false (a=0) the function getvalue is never called.
« Last Edit: September 23, 2012, 05:29:16 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

boral

  • Guest
Re: Are logical operators symmetric
« Reply #3 on: October 04, 2012, 04:33:23 PM »
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.
« Last Edit: October 04, 2012, 04:39:42 PM by boral »