Issue
Why in Kotlin bitwise operation is not possible with 4-byte hex and Int?
At the same time, there is no problem in Java
As far as I know, in JVM int
takes 4 bytes.
// Kotlin
val num = 0
val n = 0xff000000 or num // <-- 'num' ERROR
// Java
int num = 0;
int n = 0xff000000 | num; // <-- 'num' OK
Solution
0xFF000000
is a Long, not an Int. It's outside the Int range since it is using 32 bits to represent a positive number, but Int only uses 31 bits for the number, and one bit for the sign. Any six-digit hexadecimal number where the first digit is greater than 7 requires 32 bits to represent the number, so it is interpreted as a Long.
Kotlin doesn't do implicit number conversions like Java because they can easily lead to obscure bugs. You have to manually convert it to an Int.
val num = 0
val n = 0xff000000.toInt() or num
Answered By - Tenfour04
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.