Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR using mathematical operators

Tags:

java

c

xor

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.

like image 753
Mayur Avatar asked Mar 05 '10 08:03

Mayur


People also ask

What mathematical operation is XOR?

There is no mathematical equivalent as XOR is a bitwise operation. They are not related to mathematics but to how computers handle numbers.

How do you write XOR operations?

In all languages you can XOR two integers say a and b by '^' this operation. i.e to find a XOR b, just do a^b (this operation is defined in all languages).


2 Answers

(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.

like image 91
glebm Avatar answered Sep 28 '22 06:09

glebm


The simplest expression I can come up with is: a != b.

(Previous best effort was (a + b) == 1)

like image 30
Paul R Avatar answered Sep 28 '22 06:09

Paul R