Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hallo guy, please i got a problem when i try to Convert a double value in Binary

C#
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]);

    }
}


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
Posted
Comments
__John_ 7-Nov-12 9:35am    
I hope someone can answer this.
It seems the '<<' operator cannot be applyed to any type, short, int or long. Makes no sense.
__John_ 7-Nov-12 10:13am    
Try changing 'bit' from 'long' to 'int'.

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.
C#
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]);
  }
}


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.
 
Share this answer
 
v2
Comments
stefan from germany 8-Nov-12 7:41am    
Thanks Alan its run perfectly

thx guy for your help.
Stefan
I think the problem is down to the numeric types you're using. Amend your left shift to:

C#
((long)1 << bit)


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);
 
Share this answer
 
v2
Comments
stefan from germany 7-Nov-12 9:34am    
it doesn't walk i already try it Jim
jim lahey 7-Nov-12 9:42am    
Please see update
Thomas Daniels 7-Nov-12 9:36am    
If I try this, I'm getting error:
"Operator '<<' can't be applied to operands of type 'long' and 'long'".
stefan from germany 7-Nov-12 9:53am    
Jim please can u explain me i don't understand correctly what it means. how can i use it?
jim lahey 7-Nov-12 9:58am    
It means the number to the right of the "<<" needs to be an integer. the value of 'bit' either needs to be an integer or you need to rethink your bitwise shift

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900