Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between or and xor

Tags:

boolean

My question is operator related. Today I studied about the operators. Having a confusion. In PHP what is the difference between "or" and "xor". I know both of them are related to Boolean expression. But can't find the original difference.

Anyone please help to understand it more clearly.

like image 650
Provash Shoumma Avatar asked Jul 15 '13 17:07

Provash Shoumma


People also ask

Is XOR the same as exclusive or?

Exclusive or (XOR, EOR or EXOR) is a logical operator which results true when either of the operands are true (one is true and the other one is false) but both are not true and both are not false. In logical condition making, the simple "or" is a bit ambiguous when both operands are true.

HOW DO OR and XOR logic gates differ?

In other words, for the output to be 1, at least input one OR two must be 1. The XOR ( exclusive-OR ) gate acts in the same way as the logical "either/or." The output is "true" if either, but not both, of the inputs are "true." The output is "false" if both inputs are "false" or if both inputs are "true."

What is the difference between XOR and NOR gate?

XOR: XOR gate or Exclusive-OR gate is a special type of logic gate which gives 0 as output if both of the inputs are either 0 or 1, otherwise it gives 1. XNOR: XNOR gate or Exclusive-NOR gate is a special type of logic gate which gives 1 as output when both the inputs are either 0 or 1, otherwise it gives 0.

What is difference between XOR and Bitwise XOR?

The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.


2 Answers

It has to do with mutual exclusion. xor is exclusive. or is inclusive.

Truth Table Comparison

$x $y ($x or $y) ($x xor $y) 0  0    0          0 1  0    1          1 0  1    1          1 1  1    1          0 

Note: the difference in the last case. xor is only true when either $x or $y is true, but not both (as the case for or).

like image 114
Jason McCreary Avatar answered Sep 20 '22 21:09

Jason McCreary


xor means "exclusive or". That is to say, it's or, but with the single change that if both parameters to the operation are true, the answer is false.

A xor B == (A or B) && !(A and B) 
like image 44
bengoesboom Avatar answered Sep 22 '22 21:09

bengoesboom