Marcus B
Marcus B

Reputation: 41

Only 1 true of 4 inputs (Boolean Logic Expression)

Write an expression where you have 4 inputs and only output true when one and only one input is true.

So far I've thought of using XOR gates ((x0 XOR x1) XOR (x2 XOR x3)) but I need to solve the case in which we have 3 true for example x0=false x1=true x2=true x3=true, will output True XOR False and finally the whole expression True. I want to make the expression as basic as possible (ie use as few operators as possible). Ive also tried adding ((x0 XOR x1)Y(x2 XOR x3)) ∧ ¬((x0∧x1)∨(x2∧x3)) Im not sure that this is the best way to do it.

I expect the output of (x0=false x1=true x2=true x3=true) to be False but with my first expression it will be true, and something like (x0=false x1=true x2=false x3=false) to be true, which my first expression works correctly for

Upvotes: 3

Views: 3965

Answers (2)

Krishna Vyas
Krishna Vyas

Reputation: 1028

This expression gives you, your expected Output(output true when one and only one input is true.). Please test it..

(!x0 AND !x1 AND (x2 XOR x3)) OR (!x2 AND !x3 AND (x0 XOR x1))

Upvotes: 3

Kiran Maniya
Kiran Maniya

Reputation: 8979

Take a look at this demo in php(you can use it with any programming language you want). suppose you have 4 boolean variables as X1,X2,X3 andX4. you have to check for all (No of Boolean Exp)+1 combination of expressions. here you have 4 so there will be 5 combinations. Booleans so there will be a combination of expression. and the last combination is for checking at least one should be true.

$x1 = true;
$x2 = false;
$x3 = false;
$x4 = true;

if(($x1 && !$x2 && !$x3 && !$x4) || (!$x1 && $x2 && !$x3 && !$x4) || (!$x1 `&& !$x2 && $x3 && !$x4) || (!$x1 && !$x2 && !$x3 && $x4) && ($x1 && $x2 && $x3 && $x4)){
    echo 'only one variable is true';
}else{
    echo 'more then one var is true';
}`

Upvotes: 1

Related Questions