On 2009-08-07 13:01:55 -0400, Daniel Keep <daniel.keep.li...@gmail.com> said:

Michel Fortin wrote:
I always wondered why there isn't an XOR logical operator.

binary     logical
(a & b) => (a && b)
(a | b) => (a || b)
(a ^ b) => (a ^^ b)

 a | b | a ^^ b | a != b
---+---+--------+--------
 F | F |   F    |   F
 F | T |   T    |   T
 T | F |   T    |   T
 T | T |   F    |   F

That's why.

For this table to work, a and b need to be boolean values. With && and ||, you have an implicit convertion to boolean, not with !=. So if a == 1 and b == 2, an hypothetical ^^ would yeild false since both are converted to true, while != would yield false.

But I have another explanation now. With && and ||, there's always a chance that the expression on the left won't be evaluated. If that wasn't the case, the only difference in && vs. &, and || vs. | would be the automatic convertion to a boolean value. With ^^, you always have to evaluate both sides, so it's less useful.

--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/

Reply via email to