Shop OBEX P1 Docs P2 Docs Learn Events
Faster way for boolean XOR? — Parallax Forums

Faster way for boolean XOR?

Bobb FwedBobb Fwed Posts: 1,119
edited 2011-08-17 13:18 in Propeller 1
Is there a better way (there almost always is) to do a conditional XOR?
IF ((cond1 AND NOT(cond2)) OR (NOT(cond1) AND cond2))

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-17 12:34
    You could do this:
    if ((cond1 <> 0) ^ (cond2 <> 0))
    

    This applies a bitwise XOR to the true (-1) or false (0) values of the two <> 0 comparisons. ('Dunno why Spin lacks a logical XOR.)

    -Phil
  • lonesocklonesock Posts: 917
    edited 2011-08-17 12:35
    maybe "if not(a) <> not(b)" ?
    Jonathan
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-17 12:36
    I like Jonathan's idea better.

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-17 12:38
    How about "if a <> b" where a and b are Boolean values (0 or -1)? This is true only if (a is true and b is false) or (a is false and b is true).
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-08-17 13:18
    I'm I'm doing bit tests for the conditions. I would imagine (though I haven't tested it) that it is faster to just use NOT() on both the tests, than it is to == the values; so going back to Jonathan's solution.

    I tested it, it is fastest.
Sign In or Register to comment.