missing 'xor' Boolean operator
Wayne Brehaut
wbrehaut at mcsnet.ca
Wed Jul 15 13:36:21 EDT 2009
On Wed, 15 Jul 2009 13:37:22 +0200, Christian Heimes
<lists at cheimes.de> wrote:
>pdpi wrote:
>> On Jul 15, 12:08 am, Christian Heimes <li... at cheimes.de> wrote:
>>> Chris Rebert wrote:
>>>> Using the xor bitwise operator is also an option:
>>>> bool(x) ^ bool(y)
>>> I prefer something like:
>>>
>>> bool(a) + bool(b) == 1
>>>
>>> It works even for multiple tests (super xor):
>>>
>>> if bool(a) + bool(b) + bool(c) + bool(d) != 1:
>>> raise ValueError("Exactly one of a, b, c and d must be true")
>>>
>>> Christian
>>
>> "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to
>> xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor
>> 1 = 0 xor 1 = 1 if you assicate to the left)
>
>I'm well aware of the fact that I've described something differently.
>'xor' can be explained as 'check if exactly one element of two elements
>is true'. My algorithms describes a super xor, aka 'check if exactly one
>element of n elements is true'.
But that's not a "super xor" (commonly known as XOR). Rather than
describing xor as:
check if exactly one element of two elements is true
describe it as:
check if an odd number of two elements is true
then you'll get the correct definition of "super xor":
check if an odd number of n elements is true
I.e., XOR determines parity, and:
XOR <some binary vector v> =
0 if v has an even number of 1s and
1 if v has an odd number of 1s
wayne
>Christian
More information about the Python-list
mailing list