Predict the output
What does this print?
from functools import reduce
from operator import xor
print(reduce(xor, [4, 1, 2, 1, 2]), 5 ^ 5, 7 ^ 0)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4 0 7
Right: the pairs of 1 and 2 cancel, x ^ x is 0, and x ^ 0 is x.
Because order does not matter for XOR, the pairs can sit anywhere in the list and still cancel.
Why not the others?
- B 10 0 7. XOR does not add; it cancels equal bits.
- C 4 5 7. A value XOR itself is 0.