IEEE 754 decomposition
Every double-precision float is 64 bits: one sign bit, eleven exponent bits, and fifty-two mantissa bits. A normal number reconstructs as (−1)^sign × 1.mantissa × 2^(exponent − 1023) — the leading 1 is implicit and never stored. Four special cases change that formula: an all-zero exponent means either zero (mantissa also zero) or a subnormal (mantissa nonzero, and the leading bit is 0, not 1); an all-ones exponent means either infinity (mantissa zero) or NaN (mantissa nonzero).
When it is the right choice
Whenever "what does this float’s bit pattern actually mean" is the real question — reading a hex dump, debugging a serialisation format, or understanding why +0 and −0 compare equal yet have different bit patterns.
How it works
- Read the sign bit, the 11-bit exponent field and the 52-bit mantissa field directly from the 64 bits.
- If the exponent field is neither all-zero nor all-one, it is a normal number: reconstruct with an implicit leading 1 before the mantissa.
- If the exponent field is all-zero, it is a subnormal (mantissa nonzero) or zero (mantissa zero) — with no implicit leading 1 either way.
- If the exponent field is all-one, it is infinity (mantissa zero) or NaN (mantissa nonzero).
Worth noticing
- NaN is not one bit pattern — any nonzero mantissa paired with an all-ones exponent is some NaN. Different operations can and do produce different NaN bit patterns.
- The smallest positive subnormal has no implicit leading 1 at all: its entire value lives in the mantissa’s low bits.
- +0 and −0 are numerically equal (+0 === −0) but are two different bit patterns, distinguished only by the sign bit.
Cost
- Best
- O(1)
- Average
- O(1)
- Worst
- O(1)
- Extra space
- O(1)