Long Type hex ERROR


#1

I got a error in Type of Long (hex)

convert it to decimal is OK

it’s a problem?

private val POLY64REV: Long = 0x95AC9329AC4BC9B5L
private val INITIALCRC: Long = 0xFFFFFFFFFFFFFFFFL

图片


#2

The problem is that Long type in both Kotlin and Java is a signed 64-bit integer and so the values you’re trying to assign to those variables are indeed out of range.

However, it is very likely that unsigned types will be introduced into the language in the not too distant future which will solve your problem. In the meantime you could use the BigInteger type as a workaround as this can deal with integers of arbitrary size.


#3

The same code in Java is right

Kotlin version

ext.kotlin_version = ‘1.1.4-2’


#4

As stated by @alanfo, the values you use are out of range of unsigned 64-bit integers. You can use a simple function to do what you want:

fun unsignedLong(mostSignificantBits: Long, leastSignificantBits: Long) =
        (mostSignificantBits shl 32) or leastSignificantBits

Note that there is no check that only bits in the lower 32 are set in the parameters, but a few additional checks will solve that.

This results in the following code, which is a bit more verbose but still easy to read:

private val POLY64REV: Long = unsignedLong(0x95AC9329, 0xAC4BC9B5)
private val INITIALCRC: Long = unsignedLong(0xFFFFFFFF, 0xFFFFFFFF)

If you want a shorter, less cluttered notation you could try an infix extension function.