I want to perform an XOR to find if one of two booleans a and b is true but not both. Searching for XOR in Kotlin gave me this answer
infix fun xor(other: Boolean): Boolean
Performs a logical xor operation between this Boolean and the other one. source
I'm still confused on how to implement this
It's an extension that can be invoked on any Boolean
. You can use it like this:
true.xor(false)
or this:
true xor false
The last one works since the function is defined as infix
.
Other similar extensions defined on Boolean
are and
, or
and not
:
//very useful example
true.not().or(true).and(false).xor(true)
find the single number in the array that every element has a duplicate except one.
var a = 0
for (i in numsArray){
a = a xor i
}
return a
eg. input = [2,2,1] out = 1
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