在二进制中转换双精度值 [英] Convert double value in Binary

查看:111
本文介绍了在二进制中转换双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试在Binary中转换双精度值时遇到问题

hallo guy, please i got a problem when i try to Convert a double value in Binary

public void DoubleinBinaereundHexa(double wert)
{

    long bitCount = sizeof(double) * 8;
    char[] result = new char[bitCount];


    long lgValue = BitConverter.ToInt64(BitConverter.GetBytes(wert), 0);


    for (long bit = 0; bit < bitCount; ++bit)
    {
        long maskwert = lgValue & (1 << bit);
        if (maskwert > 0)
        {
            maskwert = 1;
        }


        result[bitCount - bit -1] = maskwert.ToString()[0];
    }
    Console.Write("\n\nBinaere Darstellung:\t");

    for (int i = 0; i < 64; i++)
    {

        if (i % 4 == 0)
            Console.Write(" ");
        if (result[i] == '-')
        {
            result[i] = '1';
        }
        Console.Write(result[i]);

    }
}



请我用德语对错误主义者进行道歉
Fehler 1 Der运算符''<<``kann nicht auf Operanden vom Typ``int''和``long''angewendet werden.

我认为这在英语中意味着:运算符''<<''不适用于类型为``int''和``long''
的操作数
请给别人一个主意吗?

提前thx



Please i appologyse the fault ist in German language
Fehler 1 Der Operator ''<<'' kann nicht auf Operanden vom Typ ''int'' und ''long'' angewendet werden.

i think that it means in english: Operator ''<<'' cannot be applied to operands of type ''int'' and ''long''

Please have somebody an Idea??

thx in advance

推荐答案

好,让我们拆开看看有什么问题:

我将修改代码以显示double和所有64位掩码的字节.
OK, lets take it apart and see what is wrong:

I''ll modify the code to show the bytes of the double and all 64 bit masks.
public void DoubleinBinaereundHexa(double wert) {
  int bitCount = sizeof(double) * 8;
  char[] result = new char[bitCount];

  //long lgValue = BitConverter.ToInt64(BitConverter.GetBytes(wert), 0);

  // split the conversion into two operations
  Byte[] bytes = BitConverter.GetBytes(wert);
  // show each byte
  foreach (Byte b in bytes) {
    Console.WriteLine(Convert.ToString(b, 2).PadLeft(8, '0'));
  }

  long lgValue = BitConverter.ToInt64(bytes, 0);

  for (int bit = 0; bit < bitCount; ++bit) {
    // show each mask
    Console.WriteLine(Convert.ToString((1 << bit), 2).PadLeft(64, '0'));

    long maskwert = lgValue & (1 << bit);
    if (maskwert > 0) {
      maskwert = 1;
    }
    result[bitCount - bit - 1] = maskwert.ToString()[0];
  }
  Console.WriteLine("\n\nBinaere Darstellung:");

  for (int i = 0; i < 64; i++) {

    if (i % 4 == 0)
      Console.Write(" ");
    if (result[i] == '-') {
      result[i] = '1';
    }
    Console.Write(result[i]);
  }
}



这表明(1 << bit)产生的位掩码不正确.实际上,它们是整数(32位)掩码,并且(1 << 0)和(1 << 32)给出相同的掩码.要更正此问题,字面值必须为长整数,即(1L<<位).

经过修改后,输出为



This shows that the bit masks produced by (1 << bit) are incorrect. In fact they are integer (32 bit) masks and (1 << 0) and (1 << 32) give the same mask. To correct this the literal value must be a long, i.e. (1L << bit).

With that modification the output is

0100 0000 0011 0010 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110



艾伦.



Alan.


我认为问题在于您使用的数字类型.将左移修改为:

I think the problem is down to the numeric types you''re using. Amend your left shift to:

((long)1 << bit)



在此进行更详细的讨论:

http://social.msdn.microsoft.com/Forums/zh/csharpgeneral/thread/1e9d6e3b-bbad-45df-9391-7403becd9641 [



This is discussed in greater detail here:

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/1e9d6e3b-bbad-45df-9391-7403becd9641[^]

Update: My bad, looks like I''ve lost the ability to read somewhere today. Looking at the linked article, the right hand operand of a left shift is always an int:

Shift left:
int operator <<(int x, int count);
uint operator <<(uint x, int count);
long operator <<(long x, int count);
ulong operator <<(ulong x, int count);


这篇关于在二进制中转换双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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