Search code examples
c#compact-frameworkarraysbitconverter

Interesting observation with BitConverter.GetBytes() in Compact Framework


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


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.