Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How do I convert JAVA code Float.floatToIntBits(...float var...) and Double.doubleToLongBits(... double var...) into equivalent functions in C#???

Avatar of Wally779
Wally779 asked on
4 Comments1 Solution2793 ViewsLast Modified:
Does anyone know how to convert the following two JAVA code snippets into C# ????

// Make aInt lexicographically ordered as a twos-complement int  
float A;                  
int aInt = Float.floatToIntBits(A);                                              
if (aInt < 0) aInt = 0x80000000 - aInt;

double B;
// Make bInt lexicographically ordered as a twos-complement int                  
long bInt = Double.doubleToLongBits(B);                                              
if (bInt < 0) bInt = 0x8000000000000000L - bInt;  

Basically I would like to find an equivalent function in C# of the Java functions
Float.floatToIntBits(...float var...)
Double.doubleToLongBits(... double var...)

I looked all over the internet without success. It seems that the functionFloat.floatToIntBits(A); and Double.doubleToLongBits(B); do not exist in C# or at least I cannot find a 1-1 match with and equivalent function. Maybe the answer is staring me in the face but I can't seem to figure it out. I must be brain dead right now.

Here's something I found in C# that might be useful

// Given a specific bit pattern (= 2.34):
int bits = 1075167887;

int sign = ((bits & 0x80000000) == 0) ? 1 : -1;
int exponent = ((bits & 0x7f800000) >> 23);
int mantissa = (bits & 0x007fffff);
mantissa |= 0x00800000;
// Calculate the result:
float f = (float)(sign * mantissa * Math.Pow(2, exponent - 150));

Console.WriteLine("Testing: {0}", f);
// This line prints out the value 2.34

Hence this piece of code in C# has what I am looking for only it's backwards. It converts a bit pattern into a float. However, I'm confused because it calls the input int bits = 1075167887; a "bit pattern" but I thought bit patterns were just a bunch of 0s and 1s.

Anyway, thank you very much in advance to anyone out there that can help me.
Avatar of MogalManic
MogalManicFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers