XOR using mathematical operators

Mayur picture Mayur · Mar 5, 2010 · Viewed 7.2k times · Source

How can I implement XOR using basic mathematical operators like +,-,*,/

Update: Actually, I need to track change in two matrix having Boolean values. This can be done using XORing each value with corresponding value in other matrix. But, Lp_Solve library doesn't support XOR operation. Also, it accepts only linear equations.

Answer

glebm picture glebm · Mar 5, 2010

(a − b)²

3D plot of (a − b)²

This works because:

(a − b)² = a * (a − b) + b * (b − a)

Since multiplication in ℤ₂ is conjuction (&), and 1 - a is negation (!), the above formula is equivalent to XOR for a, b ∈ {0, 1}:

(a & !b) | (b & !a)

See the comment below by Pascal Cuoq explaining why this cannot be a linear equation.