#1,093 – How Positive and Negative Zero Values Are Stored

.NET can represent both positive and negative zero floating point values.  They are stored as 32-bit floating point values as follows:

  • +0 : sign bit 0, mantissa 0 (23 bits), exponent 0 (8 bits)
  • -0: sign bit 1, mantissa 0 (23 bits), exponent 0 (8 bits)

A 32-bit positive zero is therefore stored as 00000000 (hex) and negative zero as 80000000 (hex).  We can verify this by looking at these values in memory from within Visual Studio.

Assuming the following code:

            float zero = 0.0f;
            float negZero = -0.0f;

Positive zero:

1093-001

Negative zero:

1093-002

 

Advertisements