Rate this:
See more:
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);
{
}

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'

Posted 7-Nov-12 4:16am
__John_ at 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_ at 7-Nov-12 10:13am

Try changing 'bit' from 'long' to 'int'.

Rate this:

## Solution 3

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) {
}

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

for (int bit = 0; bit < bitCount; ++bit) {

long maskwert = lgValue & (1 << bit);
}
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.
v2
stefan from germany at 8-Nov-12 7:41am

Thanks Alan its run perfectly

Stefan
Rate this:

## Solution 1

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:

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);
v2
stefan from germany at 7-Nov-12 9:34am

it doesn't walk i already try it Jim
jim lahey at 7-Nov-12 9:42am

ProgramFOX at 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 at 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 at 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
stefan from germany at 7-Nov-12 11:42am

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);
{
}

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?