0

I am developing a small communications protocol over bluetooth for an arduino to transfer data to Android. Part of the protocol sends a header that contains a line count. I have successfully read the MSB and LSB of the count and must now re-assemble that from the Int to a Short value. My declarations follow:

var iLineCountMSB: Int = 0
var iLineCountLSB: Int = 0
var sLineCountMSB: Short = 0
var sLineCount: Short = 0

I have already read the MSB and LSB from the bluetooth socket. Please note that the values are in Int, not Byte. My attempt to re-assemble the two Ints to a Short is as follows:

sLineCount = iLineCountMSB.toShort() shl 8 + iLineCountLSB.toShort()

I also copied the MSB Int value to a Short (sLineCountMSB):

sLineCountMSB = iLineCountMSB.toShort()
sLineCount = sLineCountMSB shl 8

I continue to get the same error:

-- results in:: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public inline infix fun BigInteger.shl(n: Int): BigInteger defined in kotlin

The Kotlin documentation defines the function as:

infix fun shl(bitCount: Int): Long

Am I wrong? Does this specifiy that 'Long' is the returned type? The only 'shl' that I seem to get to compile successfully is Int = Int shl 8.

  • I dont' believe this is the same issue as the answer that I have been pointed to. In that post, the user was attempting to 'and' two Bytes. I am trying to 'shl' an Int into a Short. I have tried using the base types of Short as the target and Int as the object of the 'shl' operator, and have also converted the Int object to a short via toShort(). Please explain why the two posts are equivalent. Thanks. – eranzenbach Aug 25 '20 at 14:17
  • I guess I need to do sLineCount = (iLineCountMSB shl 8).toShort() + iLineCountLSB.toShort() but that doesn't compile either and results in: Type mismatch: inferred type is Int but Short was expected – eranzenbach Aug 25 '20 at 14:52

0 Answers0