Double and <<

Hello! Please help me.
var double test = 1 << 48
logInfo(“test1”,"" + test)
Result:
[eclipse.smarthome.model.script.test1] - 65536
Why? I need 281474976710656…

I would suggest hat java work first with intergers and then cast the to double.

Maby var double = 1.0 << 48 brings the right result or some cast with long, but i am not sure if this is possible

var double = 1 as long << 48

Thomas

Thank you. And what if the code is:
var double mantisse = (bytesarray.get(6).bitwiseAnd(15)) << 48
How it is correct in this case?

Honestly - No idea

Thomas

var long mantisse = (bytesarray.get(6).bitwiseAnd(15)).longValue << 48
That’s right.

Just for an explanation, the root of the problem is that the way floating point numbers (float, double) are represented in the binary very differently from integer numbers (short, int, long). For integer numbers you basically have the highest bit represent +/- and the rest of the bits is the number’s value in base 2.

For floating point numbers you have it represented in binary using three numbers (with a fourth number being the base, which I think is base 2 in Java). To quote from the wikipedia article:

Each finite number is described by three integers: s = a sign (zero or one), c = a significand (or ‘coefficient’), q = an exponent. The numerical value of a finite number is
(−1)s × c × bq
where b is the base (2 or 10), also called radix. For example, if the base is 10, the sign is 1 (indicating negative), the significand is 12345, and the exponent is −3, then the value of the number is −11 × 12345 × 10−3 = −1 × 12345 × .001 = −12.345.

Those three numbers are all smooshed together into one big series of bits. So when you do a binary shift you are probably messing with the exponent which resulted in a smaller number.

it is one byte binary shifting. Please, see here

Java uses Big Endian so, if I’m remembering back from my college days correctly, when you did the shift you shifted off the sign bit and much of the exponent, not the coefficient like you intended.