I've been a Java Developer for many years and recently I've found something very suprising in Kotlin. In Java there is a rarely used logical operator ^
XOR but sometimes it's useful. For example: you can easly check if one and only one of two numbers is greater than zero.
With &&
AND operator and some variables a
and b
it looks like that:
boolean valid = (a > 0 && b <= 0) || (a <= 0 && b > 0);
but it can easly achieve with ^
XOR:
boolean valid = a > 0 ^ b > 0;
Now, in Kotline we don't use ^
as a XOR but just xor
and same code in Kotlin looks like that:
val valid = a > 0 xor b > 0;
And here comes a problem because this code in Kotline gives ... compilation error!! Why? Because in Java all logical operator (&,&&,|,||,^
) got lower precedence than relational operators (>, >=, <, <=, ==, !=
). Same in Koltin but it looks like not for xor
. So it goes this way:
a > 0
gives boolean valueboolean xor b > 0
first evealuated to: boolean xor b
not b > 0
The integer literal does not conform to the expected type Boolean
You can check this situation here: XOR not working well
One extra case: if you think that this one: a > 0 xor (b > 0)
works... well, no. Another compilation error: Type mismatch: inferred type is Boolean but Int was expected
Can anyone explain me is there some purpouse for such logic or it's just a bug in Kotlin language?
The xor function compares the corresponding bits of two values. If the corresponding bits are the same, it gives 0, and if they are different, it gives 1.
XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).
XOR and XNOR are examples of logical operators having the truth-tables shown in Figure 1. Figure 1 Truth-tables for XOR and XNOR. The XOR function is only true if just one (and only one) of the input values is true, and false otherwise.
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.
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
(0 xor b)
gives Int valuea > (0 xor b)
gives Boolean value(step 2 Boolean result) > 0
), but you cannot compare Boolean with IntCorrect version:
val valid = (a > 0) xor (b > 0)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With