Skip to content
systemcoding.dev
Fundamentals

How Floating-Point Numbers Are Stored: Sign, Exponent and Mantissa

Learn the IEEE 754 layout of float and double, with the sign bit, biased exponent and fraction, decode real values by hand and check them with a C program.

6 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Rust and Zig logos.

An integer stores a count. A floating-point number stores a count and a scale: it is scientific notation in binary. The value 0.15625 is held as a fraction, an exponent saying how far to shift it, and a sign, packed into 32 or 64 bits. Nearly every processor does this the same way, following the IEEE 754 standard, so the layout is worth knowing once and applies everywhere: C, Rust, Zig, Go, JavaScript and the GPU. This post takes the 32-bit float apart field by field, decodes three values by hand, prints the fields from C with GCC 14, and then lists the limits that follow from the layout. It is the base for the other floating-point posts, on rounding errors, special values, comparisons and the choice of float or double.

Scientific notation in binary

In decimal, 6,250 is written 6.25 times 10 to the power 3. The pieces are a significand, 6.25, a base, 10, and an exponent, 3. Binary does the same with base 2: a number is written as plus or minus a significand between 1 and 2, times 2 to an integer power. The significand always starts with a 1 in front of the point, so it looks like 1.0101 in binary, and the exponent says where the point really sits. The value 0.15625 is 5 over 32, which is 0.00101 in binary, and moving the point three places to the right gives 1.01 times 2 to the minus 3.

Because the leading digit is always 1, there is no need to store it. IEEE 754 keeps only the part after the point, called the fraction or the mantissa, and the leading 1 is implied. That saves a bit, so a 23-bit stored fraction gives 24 bits of precision.

The three fields

A 32-bit float, called binary32, is split like this, from the most significant bit down.

FieldBitsPurpose
sign10 for positive, 1 for negative
exponent8the power of two, stored with a bias of 127
fraction23the digits after the leading 1 of the significand

A 64-bit double, binary64, has 1 sign bit, 11 exponent bits with a bias of 1,023, and 52 fraction bits. The sign is a plain flag, unlike two's complement integers, so there are two zeros, plus and minus, as a later post covers.

The exponent is stored with a bias: the stored number is the true exponent plus 127. A stored 127 means exponent 0, a stored 128 means 1, and a stored 124 means minus 3. The bias makes the exponent an unsigned field, which keeps the ordering of positive floats the same as the ordering of their bit patterns as integers, a property that comparison and sorting tricks exploit.

For a normal number, the value is:

(-1) to the sign, times (1 plus fraction over 2 to the 23), times 2 to the (stored exponent minus 127)

Two stored exponent values are reserved. A stored exponent of 0 marks zero and the subnormal numbers, and a stored exponent of 255 marks infinity and NaN. Those cases have their own post.

Decoding 0.15625 by hand

Take 0.15625, which is 1.01 times 2 to the minus 3.

  1. The number is positive, so the sign bit is 0.

  2. The true exponent is minus 3, so the stored exponent is minus 3 plus 127, which is 124, binary 01111100.

  3. The digits after the leading 1 are 01, padded with zeros to 23 bits: 01000000000000000000000.

Putting the fields together gives 0 01111100 01000000000000000000000, regroup in nibbles as 0011 1110 0010 0000 0000 0000 0000 0000, which is 0x3E200000. The leading nibbles are 0011, then 1110, so the first byte is 0x3E. To read it back, reverse the steps: the exponent 124 minus 127 is minus 3, the significand is 1 plus a quarter, which is 1.25, and 1.25 divided by 8 is 0.15625.

Printing the fields from C

C does not let you read a float's bits directly with a cast through a pointer, since that violates the aliasing rules. The portable way is to copy the bytes into an integer with memcpy, which the compiler turns into a register move.

decode.cc
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

static void dump(float f)
{
    uint32_t u;
    memcpy(&u, &f, sizeof u);

    unsigned sign = u >> 31;
    unsigned exp  = (u >> 23) & 0xFFu;
    uint32_t frac = u & 0x7FFFFFu;

    printf("%-10g 0x%08" PRIX32 "  sign=%u exp=%3u (unbiased %4d) frac=0x%06" PRIX32 "\n",
           f, u, sign, exp, (int)exp - 127, frac);
}

int main(void)
{
    dump(0.15625f);
    dump(1.0f);
    dump(-2.5f);
    dump(0.1f);
    return 0;
}

Each field comes out by shifting and masking, as in the earlier posts on bit manipulation: the top bit, the next eight, and the low 23. The %g conversion prints the float, which is promoted to double in the call.

Text
0.15625    0x3E200000  sign=0 exp=124 (unbiased   -3) frac=0x200000
1          0x3F800000  sign=0 exp=127 (unbiased    0) frac=0x000000
-2.5       0xC0200000  sign=1 exp=128 (unbiased    1) frac=0x200000
0.1        0x3DCCCCCD  sign=0 exp=123 (unbiased   -4) frac=0x4CCCCD

The first line matches the hand decode. The second shows that 1.0 has a zero fraction and the stored exponent 127. The third, minus 2.5, is 1.01 times 2 to the 1 with the sign bit set, so the top nibble is C. The last line is the one to study: 0.1 has the fraction 4CCCCD, a repeating pattern of C digits that was rounded up at the end to D. The value 0.1 has no finite binary expansion, so the stored number is only the nearest float, a fact the next post explains.

What the layout implies

The format determines every limit you will meet.

Propertyfloatdouble
total bits3264
significand bits, including the implied one2453
exponent range of normal numbersminus 126 to 127minus 1,022 to 1,023
decimal digits that always survive a round trip615
machine epsilon2 to the minus 232 to the minus 52

Machine epsilon is the gap between 1.0 and the next larger float, which is one unit in the last place of the fraction at that scale. The header float.h exposes these as FLT_DIG, FLT_EPSILON, FLT_MAX, FLT_MIN and the DBL_ forms.

limits.cc
#include <float.h>
#include <stdio.h>

int main(void)
{
    printf("FLT_DIG %d, DBL_DIG %d\n", FLT_DIG, DBL_DIG);
    printf("FLT_EPSILON %g, DBL_EPSILON %g\n", FLT_EPSILON, DBL_EPSILON);
    printf("FLT_MAX %g, DBL_MAX %g\n", FLT_MAX, DBL_MAX);
    printf("FLT_MIN %g, DBL_MIN %g\n", FLT_MIN, DBL_MIN);
    printf("(float)16777217 = %.0f\n", (double)(float)16777217);
    return 0;
}

The last line shows the precision limit of a float. It holds every integer up to 2 to the 24, which is 16,777,216, exactly. Above that, the spacing between floats is 2, so 16,777,217 is not representable, and it rounds to the nearest even neighbour.

Text
FLT_DIG 6, DBL_DIG 15
FLT_EPSILON 1.19209e-07, DBL_EPSILON 2.22045e-16
FLT_MAX 3.40282e+38, DBL_MAX 1.79769e+308
FLT_MIN 1.17549e-38, DBL_MIN 2.22507e-308
(float)16777217 = 16777216

Here FLT_MIN is the smallest positive normal float, not the smallest positive value: subnormal numbers go lower, down to about 1.4 times 10 to the minus 45. Spacing grows with magnitude. Floats are dense near zero and sparse far from it: between 1 and 2 there are 2 to the 23 evenly spaced floats, and between 2 to the 20 and 2 to the 21 there are the same number, spread over a much wider range.

Why this design

The exponent field gives a huge range, from about 10 to the minus 38 to 10 to the 38 in 32 bits, at the cost of fixed relative precision. Every float is accurate to about one part in 16 million, whether it is 0.001 or 1,000,000. That is the right shape for science and graphics, where relative error matters, and the wrong shape for quantities with fixed absolute precision such as money, where a count of cents in an integer is exact.

The layout also makes some operations cheap. Comparing two positive floats as integers gives the right order, so sorting floats can use integer comparison with a small fix for negatives. Doubling a float is adding one to its exponent field. Hardware multiplies the significands, adds the exponents and renormalises.

Common mistakes

  • Reading the stored exponent as the real exponent. Subtract the bias, 127 for float and 1,023 for double.

  • Forgetting the implied leading 1 and decoding the fraction as the whole significand.

  • Using a pointer cast, *(uint32_t *)&f, to inspect bits. It violates strict aliasing, so use memcpy or a union.

  • Treating floats as evenly spaced. The spacing doubles each time the exponent increases by one.

  • Expecting integers above 2 to the 24 to survive in a float, or above 2 to the 53 in a double.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.