Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is XOR operator in Kotlin a feature or a bug?

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:

  1. a > 0 gives boolean value
  2. boolean xor b > 0 first evealuated to: boolean xor b not b > 0
  3. And finally we got compilation error that said: 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?

like image 700
Michał Kupisiński Avatar asked Feb 03 '20 06:02

Michał Kupisiński


People also ask

What is XOR in Kotlin?

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.

What kind of operator is XOR?

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).

Is XOR a logical operator?

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.

Does C++ have XOR?

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

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)
like image 106
IR42 Avatar answered Oct 18 '22 00:10

IR42


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).

like image 28
triyambkam Avatar answered Oct 18 '22 01:10

triyambkam