3

Is there Kotlin analogue to Java ^= (xor and assign) operator?

For instance, in Java we can simply write

a ^= b
// a = a ^ b

It looks like in Kotlin we need to assign value explicitly

a = a xor b

Is it possible to avoid duplication of variable a in Kotlin code?

yanefedor
  • 2,132
  • 1
  • 21
  • 37
  • Does this answer your question? [What is the Kotlin exponent operator](https://stackoverflow.com/questions/50270435/what-is-the-kotlin-exponent-operator) – MMG Apr 02 '20 at 02:37
  • https://stackoverflow.com/questions/50270435/what-is-the-kotlin-exponent-operator – MMG Apr 02 '20 at 02:37
  • No, unfortunately there is no version of Java’s ^= or |=. – Tenfour04 Apr 02 '20 at 04:17

1 Answers1

4

Unlike C, C++ and Java, Kotlin doesn’t have bitwise operators like |(bitwise-or), &(bitwise-and), ^(bitwise-xor), << (signed left shift), >>(signed right shift) etc.

For performing bitwise operations, Kotlin provides following methods that work for Int and Long types -

  • shl - signed shift left (equivalent of << operator)
  • shr - signed shift right (equivalent of >> operator)
  • ushr - unsigned shift right (equivalent of >>> operator)
  • and - bitwise and (equivalent of & operator)
  • or - bitwise or (equivalent of | operator)
  • xor - bitwise xor (equivalent of ^ operator)
  • inv - bitwise complement (equivalent of ~ operator)