Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR from only OR and AND

How do you do the XOR bitwise operation if you only have available the AND and the OR operations?

like image 299
John Zane Avatar asked Jan 17 '11 16:01

John Zane


People also ask

Is XOR if and only if?

Exclusive or or exclusive disjunction is a logical operation that is true if and only if its arguments differ (one is true, the other is false).

Can we implement XOR gate using only NOT and and OR gates?

The Boolean expression for XOR gate cannot determined directly like AND, OR gates. As it is a Hybrid gate, the Boolean expression of output of XOR gate is given by a combining of Multiplication, Addition and inverting of inputs.

Can XOR have 3 inputs?

For 3 or more inputs, the XOR gate has a value of 1when there is an odd number of 1's in the inputs, otherwise, it is a 0. Notice also that the truth tables for the 3-input XOR and XNOR gates are identical.


2 Answers

(a XOR b) = ((a OR b) - (a AND b)), or in other words, the union set minus the intersection set.

Code example (in javascript):

var a = 5;
var b = 12;
var xor = (a | b) - (a & b); // result: 9
like image 89
Zack Avatar answered Feb 07 '23 01:02

Zack


Creating my own scripting language - ChrisScript - you just need something like:

#!/bin/chrish

bit XOR (bit A, bit B)
{
   bit notA;
   bit notB;

   IF (A == 0) notA = 1 ELSE notA = 0;
   IF (B == 0) notB = 1 ELSE notB = 0;

   F = ((A && notB) || (notA && B));

   RETURN F;
}

Even without NOT, it can be emulated like this. But this is the best solution you're going to get without having some form of inverter. I find it hard to believe you don't have some form of inverter availble -- what scripting environment are you using?

like image 21
Chris J Avatar answered Feb 07 '23 01:02

Chris J