XOR using mathematical operators

前端 未结 9 1614
北海茫月
北海茫月 2021-02-05 09:57

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

Update: Actually, I need to track change in two matrix having Boolean values. T

9条回答
  •  死守一世寂寞
    2021-02-05 10:12

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

提交回复
热议问题