Is XOR operator in Kotlin a feature or a bug?

xor (being an infix function-docs) in Kotlin has lower precedence than the arithmetic operators(*, /, %,+, -) and has higher precedence than Comparison(<, >, <=, >=),Equality(==, !==) & Assignment(=, +=, -=, *=, /=, %=) operators.(check for full reference for precedence here).


xor is not an operator, but an infix function. Infix function calls have higher precedence than the comparison. Expressions

val valid = a > 0 xor b > 0 is the same as val valid = a > (0 xor b) > 0

  1. (0 xor b) gives Int value
  2. a > (0 xor b) gives Boolean value
  3. and it turns into a comparison between Boolean and Int ((step 2 Boolean result) > 0), but you cannot compare Boolean with Int

Correct version:

val valid = (a > 0) xor (b > 0)