I am developing a portable class library in C# and I want to bit convert a double
to a long
. The most straightforward solution to this issue would be to use the BitConverter.DoubleToInt64Bits
method, but unfortunately this method is not available in the Portable Library subset of the .NET class library.
As an alternative I have come up with the following "two-pass" bit conversion:
var result = BitConverter.ToInt64(BitConverter.GetBytes(x), 0);
My tests show that this expression consistently produces the same result as DoubleToInt64Bits
. However, my benchmark tests also show that this alternative formulation is approximately four times slower than DoubleToInt64Bits
when implemented in a full .NET Framework application.
Using only the Portable Library subset, is it possible to implement a replacement of DoubleToInt64Bits
that is quicker than my formulation above?
DoubleToInt64Bits
method, but more than twice as fast as my "two-pass" solution. For a while I was a little bit worried that this would not work in a portable library, since there is no indication on MSDN that the Portable Library subset supports the FieldOffset attribute. However, the implementation works so it only seems to be an oversight in the MSDN documentation. – Peneus