2

I am trying to write a boolean expression in Python, but it appears Python can only do XOR expressions with bit operations.

What would be the best way to go about writing this expression in Python without the XOR operator.

(A ^ B ^ C ^ D) U ((B U C U D)' XOR A)

EDIT:

I've attempted this:

if (A and B and C and D) or ((A and not (B or C or D)) or (not A and (B and C and D))):

and I'm looking to simplify it.

Bob
  • 746
  • 3
  • 11
  • 26
  • 4
    what have you attempted? – depperm Jul 18 '16 at 18:51
  • Check out the [python wiki](https://wiki.python.org/moin/). There is a good page on [bitwise operators](https://wiki.python.org/moin/BitwiseOperators). – pat Jul 18 '16 at 19:00
  • With some modifications, you can practically write this out. One idea is to use the set library if you do not want to use binary expressions. – af3ld Jul 18 '16 at 19:01

2 Answers2

3

Just use the bitwise ^ operator. Python's booleans return a boolean when ^-ed together:

>>> True ^ True
False
>>> True ^ False
True

The and and or operators exist primarily to support short-circuiting, but XOR can't short-circuit.

There's also !=:

>>> True != True
False
>>> True != False
True

but that doesn't do what you want when chained with more arguments:

>>> True != True != True
False
user2357112
  • 260,549
  • 28
  • 431
  • 505
1

(A and B and C and D) or ((A and not (B or C or D)) or (not A and (B and C and D))) will simplify to this: (B and C and D) or (A and not (B or C or D))

Paluter
  • 73
  • 7