Bitwise with doubles

I am porting some Java code where there is a lot of bit masking.

In Java, you can binary AND and OR between two Double values but in Xojo the compiler complains that they are Integer operations. Is there a way to do this in Xojo?

AND / OR byte per byte? Just a thought…

[code]Public Function DoubleToBytes(DoubleValue as Double) as Byte()
Var m As New MemoryBlock(8)
m.LittleEndian = True
m.DoubleValue(0) = DoubleValue

Dim returnValue(7) as Byte
returnValue(0) = m.Byte(0)
returnValue(1) = m.Byte(1)
returnValue(2) = m.Byte(2)
returnValue(3) = m.Byte(3)
returnValue(4) = m.Byte(4)
returnValue(5) = m.Byte(5)
returnValue(6) = m.Byte(6)
returnValue(7) = m.Byte(7)

return returnValue

End Function[/code]

And / OR 8 bytes at once for faster results. m.UInt64Value(0) = x

[quote=473093:@Garry Pettet]I am porting some Java code where there is a lot of bit masking.

In Java, you can binary AND and OR between two Double values but in Xojo the compiler complains that they are Integer operations. Is there a way to do this in Xojo?[/quote]
Are you sure that is possible in Java?
Performing bitwise operations on floating point numbers doesn’t make sense to me as AND / OR / NOT operations on the individual bits / bytes of a floating number won’t give you the same result as performing the same actions on an integer.
Maybe Java internally casts the floating point numbers to integers to do this.

I don’t know what is he doing but sometimes complex things like cryptography and compression can use floating point to “calculate bits” needing to be put into a stream, and it needs casting, masks and such, and that’s not the equivalent of “bitwise ops between floating point values”. Seems a bit weird, but… who knows.

After a little digging through Java’s standard library definitions, it looks like @Kevin Gale is right. Internally Java converts the double to a long which is a 64-bit two’s complement integer which (I think) is the same as Xojo’s Int64 data type.

From their docs:

Is this what happens with a simple cast from Double to Int64 in Xojo?

[quote=473128:@Garry Pettet]After a little digging through Java’s standard library definitions, it looks like @Kevin Gale is right. Internally Java converts the double to a long which is a 64-bit two’s complement integer which (I think) is the same as Xojo’s Int64 data type.

From their docs:

Is this what happens with a simple cast from Double to Int64 in Xojo?[/quote]
Casting would just strip the decimal part.

If Java is using doubleToLongBits() to perform the conversion I think this would be the equivalent.

  Dim d As double
  Dim m As MemoryBlock
  Dim i As Int64
  
  m = New MemoryBlock(8)
  
  d = 1.5
  
  'convert the value
  m.DoubleValue(0) = d
  i = m.Int64Value(0)
  
  
  '**** do something with i
  
  
  'convert the value back
  m.Int64Value(0) = i
  d = m.DoubleValue(0)
  
  break

When working with bits, you’ll want to use unsigned integer types like UInt64.

The layout for an integer is straightforward, just bytes interpreted according to LittleEndian properties. The layout for a double is much different. The first few bits represent sign and a starting point, with each subsequent bit representing a power of 2 decremented from the starting point. There are special sequences that represent positive and negative infinity, and NaN.

Doing bitwise operations directly against this format wouldn’t make sense.

[quote=473093:@Garry Pettet]I am porting some Java code where there is a lot of bit masking.

In Java, you can binary AND and OR between two Double values but in Xojo the compiler complains that they are Integer operations. Is there a way to do this in Xojo?[/quote]

why not just stuff the double into a memory block that is 8 bytes and pull it out using the Uint64 or Int64
do the bitwise operation on the Uint64 / Int64
put the uint64/Int64 back in the memoryblock and pull out the double value

exactly as kevin suggested

Out of curiosity Kem, why use unsigned instead of signed?

makes no difference until / unless you want to use it as a numerical value
you’re not - just a container for bits

For sanity’s sake … When you set a couple of bits and add up the corresponding powers of 2 you would expect some large or small but always positive value, but with a signed integer you could end up with a negative one. As long as you only deal with individual bits it doesn’t make a difference though.

Depending on how you manipulate the bits, it could make a difference since the first bit represents sign instead of a value. If all you’re doing is applying masks, fine. If you start bit-shifting by multiplying or dividing by powers of 2, you risk getting incorrect results.

There is no downside to using the unsigned version and, as Michael said, it will help you keep your sanity.

if you’re doing bitwise shits using powers of 2 then you’re applying math to a bitwise operations and then it matters

if you just use bitwise shifts its not “math” and makes no difference because 8, 16, 32 or 64 bits have no inherent interpretation as signed or unsigned

&hFEEDBEEF isnt a signed or unsigned 32 bit value - its just a pile of bits

now if I told you it was an Unsigned 32 bit value you would know how to treat the bits
But if I said it was a single then those exact same bits mean something completely different

How did you manage to post that without the CensorBot catching it?

no idea - and I cant even edit it now :stuck_out_tongue:

You can try BigNumberMBS class in MBS Plugin.
A 320 bit floating point and it has a bitAnd operator.