GVKun编程网logo

c# – .NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢

43

本篇文章给大家谈谈c#–.NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢,同时本文还将给你拓展32-bitArmCortex-M3microcontrol

本篇文章给大家谈谈c# – .NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢,同时本文还将给你拓展32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Ho...、693. Binary Number with Alternating Bits -LeetCode、c# – 为什么Convert.ToDateTime(Int64)失败?、c# – 如果数据未在给定的偏移处对齐,为什么BitConverter.ToInt32一次读取一个字节?等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

c# – .NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢

c# – .NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢

我正在用C#开发一个可移植的类库,我希望将double转换为long.这个问题最直接的解决方案是使用BitConverter.DoubletoInt64Bits方法,但不幸的是,这个方法在.NET类库的Portable Library子集中不可用.

作为替代方案,我提出了以下“双通”位转换:

var result = BitConverter.ToInt64(BitConverter.GetBytes(x),0);

我的测试表明,这个表达式始终产生与DoubletoInt64Bits相同的结果.但是,我的基准测试还表明,在完整的.NET Framework应用程序中实现时,此替代公式比DoubletoInt64Bits慢大约四倍.

仅使用可移植库子集,是否可以实现比上面的公式更快的DoubletoInt64Bits替换?

解决方法

使用工会怎么样?
[StructLayout(LayoutKind.Explicit)]
public struct DoubleLongUnion
{
    [FieldOffset(0)]
    public double Double;

    [FieldOffset(0)]
    public long Long;
}

public static long DoubletoInt64Bits(double value)
{
    var union = new DoubleLongUnion {Double = value};
    return union.Long;
}

32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Ho...

32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Ho...

点击获取完整资源:32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Host/Device/OTG, CAN

The LPC1769/68/67/66/65/64/63 are Arm Cortex-M3 based microcontrollers for embedded applications operating at CPU frequencies of up to 100 MHz.

点击获取完整资源:32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Host/Device/OTG, CAN

693. Binary Number with Alternating Bits -LeetCode

693. Binary Number with Alternating Bits -LeetCode

Question

693. Binary Number with Alternating Bits

Solution

思路:输入一个整数,它的二进制 01 交替出现,遍历其二进制字符串,下一个与上一个不等,返回 true, 如果有相等的就返回 false

Java 实现:

public boolean hasAlternatingBits(int n) {
    char last = ''2''; // 非0非1即可
    for (char c : Integer.toBinaryString(n).toCharArray()) { // int转二进制字符串
        if (c == last) return false;
        last = c;
    }
    return true;
}

c# – 为什么Convert.ToDateTime(Int64)失败?

c# – 为什么Convert.ToDateTime(Int64)失败?

为什么以下失败/“从’Int64’到’DateTime’的无效转换.”例外?
long oldDate=new DateTime(2015,1,1).Ticks;
DateTime newDate=Convert.ToDateTime(oldDate);

.Ticks是long / Int64,Convert.ToDateTime(Int64)MSDN文档显示接受long / Int64的方法.

public static DateTime ToDateTime(
  long value
)

编辑:
如下面的ebyrob所指出的那样应该是:

long oldDate=new DateTime(2015,1).Ticks;
DateTime newDate=new DateTime(oldDate);

解决方法

从Convert.ToDateTime方法(Int64)上的MSDN文档:

Calling this method always throws InvalidCastException.

Return Value
Type: System.DateTime
This conversion is not supported. No value is returned.

我不确定为什么不支持这一点,特别是如果新的DateTime(oldDate)运行良好:

DateTime newDate = new DateTime(oldDate);

c# – 如果数据未在给定的偏移处对齐,为什么BitConverter.ToInt32一次读取一个字节?

c# – 如果数据未在给定的偏移处对齐,为什么BitConverter.ToInt32一次读取一个字节?

很抱歉这个令人困惑的标题,但我想不出更好的解释方法.

最近浏览the source code的BitConverter时,我遇到了一段奇怪的代码:

public static unsafe int ToInt32(byte[] value,int startIndex)
{
    fixed (byte* pbyte = &value[startIndex])
    {
        if (startIndex % 4 == 0) // data is aligned 
            return *((int*)pbyte);
        else
        { 
            if (IsLittleEndian)
            {  
                return (*pbyte) | (*(pbyte + 1) << 8)  | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24); 
            } 
            else
            { 
                return (*pbyte << 24) | (*(pbyte + 1) << 16)  | (*(pbyte + 2) << 8) | (*(pbyte + 3));                         
            } 
        }
    }
}

在这种情况下,如何将pbyte转换为int *(第6行)会违反数据对齐?为了简洁,我把它留了下来,但代码有正确的参数验证,所以我很确定它不能是内存访问违规.铸造时会失去精度吗?

换句话说,为什么不能将代码简化为:

public static unsafe int ToInt32(byte[] value,int startIndex)
{
    fixed (byte* pbyte = &value[startIndex])
    {
        return *(int*)pbyte;
    }
}

编辑:Here是有问题的代码部分.

解决方法

我敢打赌,这与C#规范5.0版本中的§18.4的这一部分有关(强调我的):

When one pointer type is converted to another,if the resulting pointer is not correctly aligned for the pointed-to type,the behavior is undefined if the result is dereferenced.

在“未对齐”的情况下进行字节复制是为了避免依赖于明确未定义的行为.

关于c# – .NET可移植库缺少BitConverter.DoubleToInt64Bits,替换速度很慢的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于32-bit Arm Cortex-M3 microcontroller; up to 512 kB flash and 64 kB SRAM with Ethernet, USB 2.0 Ho...、693. Binary Number with Alternating Bits -LeetCode、c# – 为什么Convert.ToDateTime(Int64)失败?、c# – 如果数据未在给定的偏移处对齐,为什么BitConverter.ToInt32一次读取一个字节?等相关内容,可以在本站寻找。

本文标签: