12,999,760 members (40,920 online)
Rate this:
Please Sign up or sign in to vote.
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);
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:16am
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'.

## 2 solutions

Rate this:
Please Sign up or sign in to vote.

## 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) {
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.
v2
Comments
stefan from germany 8-Nov-12 7:41am

Thanks Alan its run perfectly

thx guy for your help.
Stefan
Rate this:
Please Sign up or sign in to vote.

## 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:

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);
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
ProgramFOX 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
stefan from germany 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);
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)

Top Experts
Last 24hrsThis month
 RickZeeland 185 OriginalGriff 50 Michael_Davies 40 Bharat Leel 35 Jochen Arndt 30
 OriginalGriff 4,547 ppolymorphe 2,292 RickZeeland 2,142 Kornfeld Eliyahu Peter 1,973 Richard Deeming 1,593

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100