Question

I am trying to convert a float value to byte array using BitConverter.GetBytes(...) method. I came across one interesting issue. Consider following code -

float f = 0.0;
byte[] bytes = BitConverter.GetBytes(f);
for (int j = 0; j < bytes.Length; j++)
{
     Trace.Write(bytes [j] + " ");
} 

Above conversion returns array of 4 bytes which I am printing on console. However, i noticed that sometimes the output of 4 bytes is {0 0 0 128} instead of {0 0 0 0}.

Any help?

Thanks, Omky

Was it helpful?

Solution

As written here http://en.wikipedia.org/wiki/Signed_zero

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0

What you have found is the negative zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top