如何获得“双”的位作为“长” [英] How to get the bits of a "double" as a "long"

查看:176
本文介绍了如何获得“双”的位作为“长”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想操纵C#中浮点数的按位表示。 BinaryWriter和BinaryReader这样做:

pre $ public $ unsafe void void Write(double value)

ulong num = *((ulong *)& value);
...

public virtual unsafe double ReadDouble()
{
...
ulong num3 = ...;
return *((double *)& num3);





$ b

有没有办法做到这一点不安全的代码,没有开销实际上使用BinaryWriter和BinaryReader?

解决方案

你试图完全避免不安全的代码,或者你只是想要替代那些特定的方法在 BinaryReader BinaryWriter



BitConverter.DoubleToInt64Bits BitConverter.Int64BitsToDouble ,它们被设计成完全符合你的需求,尽管我认为它们在后台使用和 BinaryReader / BinaryWriter 方法。


I would like to manipulate the bitwise representation of floating-point numbers in C#. BinaryWriter and BinaryReader do it this way:

public virtual unsafe void Write(double value)
{
    ulong num = *((ulong*) &value);
    ...
}
public virtual unsafe double ReadDouble()
{
    ...
    ulong num3 = ...;
    return *((double*) &num3);
}

Is there a way to do this without unsafe code, and without the overhead of actually using BinaryWriter and BinaryReader?

解决方案

Are you trying to avoid unsafe code altogether, or do you just want an alternative to those specific methods on BinaryReader and BinaryWriter?

You could use BitConverter.DoubleToInt64Bits and BitConverter.Int64BitsToDouble, which are designed to do exactly what you need, although I think they use the same unsafe conversion behind-the-scenes as the BinaryReader/BinaryWriter methods.

这篇关于如何获得“双”的位作为“长”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆