How do you do the XOR bitwise operation if you only have available the AND and the OR operations?
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).
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.
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.
(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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With