missing 'xor' Boolean operator

Robert Kern robert.kern at gmail.com
Wed Jul 15 14:47:28 EDT 2009


On 2009-07-15 13:29, Wayne Brehaut wrote:
> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson
> <dickinsm at gmail.com>  wrote:
>
>> On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman"<pfeld... at verizon.net>
>> wrote:
>>> Current Boolean operators are 'and', 'or', and 'not'.  It would be nice to
>>> have an 'xor' operator as well.
>> Hmm.  I don't think 'nice' is sufficient.  You'd need to make the case
>> that it's sufficiently useful to justify adding a new keyword 'xor' to
>> the language;  I suspect that would be an uphill struggle. :)
>>
>> I'll just note that:
>>
>> (1) It's easy to emulate xor:  'x xor y'<->  bool(x) != bool(y)
>>
>> (2) 'and' and 'or' are special in that they have useful short-
>> circuiting behaviour; xor doesn't have this property (that is, you
>> always need to evaluate *both* operands to determine the result).
>>
>> I'd also guess that 'xor' would be much less used than 'and' or 'or',
>> but maybe that's just a reflection of the sort of code that I tend to
>> write.
>
> You're right about that!. It's used everywhere in:
>
> - coding and encryption theory (and practice) (e.g.,
> http://www.mathcs.emory.edu/~whalen/Hash/Hash_Articles/IEEE/XOR-based%20hash%20functions.pdf)
> - emulation and simulation of hardware (since all but the most trivial
> logic circuits are likely to include XOR-gates)
> - hence, for design of new architectures or simulators or virtual
> machines and simplification of existing ones--(e.g.,
> http://www.date-conference.com/archive/conference/proceedings/PAPERS/1999/DATE99/PDFFILES/05A_6.PDF)
> and
> http://bochs.sourceforge.net/Virtualization_Without_Hardware_Final.pdf
> which includes:
>
> "The answer relies on the observation that subtracting an integer
> value from 0xFFFFFFFF gives the same result as XOR-ing that same value
> to 0xFFFFFFFF."
>
> And, perhaps the most useful use of all, for Bouton's solution of the
> game of Nim--both for the proof that his strategy "solves" the game
> and for an easy implementation of a Nim-playing program--and the only
> operator needed is XOR (e.g., http://www.wordiq.com/definition/Nim).

All of those can use the bitwise XOR operator, not the boolean XOR. Python 
already has the ^ operator for those purposes.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list