Email conversation
From | LTH |
To | Me |
Subject | A more common XOR idiom |
Date | 21 June 2007 19:40 |
Hi,
I just saw your writeup on xor. The standard idiom for (a XOR b) in
languages that do not support XOR directly is (!a != !b): ! produces
boolean from the expression, and if x and y are boolean x != y is the
same as XOR. If both a and b are known to produce boolean values, the
! can be removed too: (a != b).
The existence of these idioms is one of the reasons ^^ was left out as
an operator in ECMAScript 4, even though ActionScript 3 supports it.
(Another reason is that XOR, unlike AND or OR, must evaluate both
operands, thus adding syntax does not provide extra expressive power.)
--lars
From | Me |
To | LTH |
Subject | Re: A more common XOR idiom |
Date | 21 June 2007 21:32 |
Lars,
> The standard idiom for (a XOR b) in
> languages that do not support XOR directly is (!a != !b)
Thanks for this. It's a solution I have not seen before, and yes, it solves
the various problems with simplicity. Should have thought of it myself ...
well, I did, but then got put off by the missing ! which I really should
have thought to include to allow for non-booleans (specifically the
expressions).
Will update the article.
Cheers
Tarquin.