Take the 2-minute tour ×

Apparently there is no boolean version of the bitwise exclusive-or operator (^^^)... what to do?

share|improve this question
add comment

2 Answers

up vote 18 down vote accepted

This is provided by the not-equal operator <>.

share|improve this answer
Okay. I'm stumped. How is <> a replacement for boolean xor? –  Daniel Oct 5 '11 at 2:57
<> is True when the inputs are different, and False when the inputs are the same. This is exactly what the xor operation does. –  Greg Hewgill Oct 5 '11 at 2:59
Of course! I asked that too hastily. Makes you wonder why C# has it. In F#, one slight difference an xor operator would make is inferring its operands to be of type bool. –  Daniel Oct 5 '11 at 3:23
Doy, yes this works, thanks. But I see using <> results in the F# compiler emitting something like call x; call y; ceq; ldc.i4.0; ceq while the C# compiler will emit call x; call y; xor for ^... I wonder if there are (micro) performance implications there. –  Stephen Swensen Oct 5 '11 at 3:28
It is possible to emit the xor instruction using inline IL, but this feature is deprecated and they've told us they intend on removing it in the next version: let inline xor (x:bool) (y:bool) = (# "xor" x y : bool #) –  Stephen Swensen Oct 5 '11 at 3:38
add comment
let inline xor a b = (a || b) && not (a && b)
share|improve this answer
add comment

Your Answer


