Does Int has something akin to java.lang.Integer.SIZE?

Int has the same MAX_VALUE and MIN_VALUE properties as java.lang.Integer but doesn’t appear to have a SIZE equivalent (the number of bits). I’m doing my bit arithmetic with Int’s but referencing java.lang.Integer.SIZE, which looks a bit wacky but it was that or code it as 32.

java.lang.Integer has a lot of good stuff in there. I’ve been making use of methods like numberOfTrailingZeros(), rotateRight() and bitCount(). There’s some heavy-duty science in those functions, which matters for low-latency applications. The last of those, bitCount(), making use of the Hamming Weight / SWAR algorithm or possibly the popcount x86 machine instruction if available. If would be great to have this hard-core science added to the scalar integer types in Kotlin.

Check out the section “New constants in companion objects of basic types” in the 1.3-M1 blog post:

Byte, Short, Int, Long, Char now have SIZE_BITS and SIZE_BYTES constants, telling how many bits or bytes a value of that type takes in binary form.

@Alexey.Belkov, you’re a star! :slightly_smiling_face: Thank you, just what I was looking for.

1 Like