Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0
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]);
 
            }
        } 
 
 
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 7 Nov '12 - 3:16

Comments
__John_ - 7 Nov '12 - 9:35
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:13
Try changing 'bit' from 'long' to 'int'.

2 solutions

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]);
  }
}
 
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.
  Permalink  
Comments
stefan from germany - 8 Nov '12 - 7:41
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:
 
((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);
  Permalink  
Comments
stefan from germany - 7 Nov '12 - 9:34
it doesn't walk i already try it Jim
jim lahey - 7 Nov '12 - 9:42
Please see update
ProgramFOX - 7 Nov '12 - 9:36
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:53
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:58
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
stefan from germany - 7 Nov '12 - 11:42
when i Change bit to int it run without problem but i get a wrong result like this: for (int 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]); } } /* compiler run without fault but i get at the end a Wrong result */ For Example: Double wert = 18, 4 /* Wrong result ist : 1110 0000 0000 0000 0000 0000 0000 0000 1110 0000 0000 0000 0000 0000 0000 0000 Normaly a correct result muss looking like this: 0100 0000 0011 0010 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110 0110 */ please what i make wrong where is the Problem?

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 464
1 Mahesh Bailwal 373
2 Maciej Los 255
3 Rohan Leuva 175
4 CPallini 175
0 Sergey Alexandrovich Kryukov 9,402
1 OriginalGriff 7,204
2 CPallini 3,933
3 Rohan Leuva 3,211
4 Maciej Los 2,743


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid