Why ^ is not allowed is Kotlin? How would we calcu...
# announcements
c
Why ^ is not allowed is Kotlin? How would we calculate 2 ^ 3 in Kotlin? I found only Double and Float has pow() but not Int. Is it the only way to solve as 2.toDouble().pow(3.toDouble()).toInt() Need some help
s
Need some help
we know. thats why you’re posting a question in #C0922A726
the shortest way to write your function would probably be
Copy code
2.0.pow(3)
with a
.toInt()
if you need it - but maybe the result would be better represented as a long or a BigInteger, depends on the size of your input, and the compiler can’t account for that
if you peek into its definition, you’ll find that
kotlin.math.pow
just delegates to
java.lang.Math
on the JVM
and if you google
integer power java
you get this stackoverflow post https://stackoverflow.com/questions/8071363/calculating-powers-of-integers
c
In Java we have an equivalent ^ symbol and can calculate 2 ^ 3 easily.
s
I’m sorry, what does
System.out.println(2 ^ 3);
print out on your machine
d
Perhaps you are referring to the xor operator? Which is just
xor
in Kotlin.
👆 2
c
Oh I was mis interpreting xor as pow. My mistake. Thanks for help.
b
One more source of confusion in Java that Kotlin clears up!
k
By making bitwise operations annoyingly verbose.
4