Inside a C float

Predict the four bytes of -13.25f, then check them with a C program.

Start with powers of two

The binary point separates whole powers of two from fractional powers. The first position to its right is worth 1⁄2, the second 1⁄4, and the third 1⁄8. Each step right halves the weight.

1101.012 = 8 + 4 + 1 + 1/4 = 13.25

Move the point until the first 1 is the only digit before it. Here it moves three places left, so multiplying by 23 restores the original value. Moving three places right would call for 2−3. This is normalization, writing the value with a single leading 1 and a separate power of two.

1101.012 = 1.101012 × 23

The significand is the whole multiplier 1.101012. Its fraction is the part after the binary point, 0.101012 or 21/32. The exponent is 3, so the scale is 23. With the same significand and exponent 2, the value is half as large.

NIST §3.1, normalized binary values

Pack the 32-bit word

On the course's Linux x86-64 C target, a stored float uses binary32, a 32-bit format defined by IEEE 754. A normal value uses an implied leading 1 and a stored exponent code from 1 through 254.

  1. Use sign bit s = 1 for a negative value, or 0 for a positive value.
  2. Store B = e + 127. The fixed offset 127 is the bias. It lets the exponent field hold an unsigned code even when the actual exponent e is negative. Here, B = 130 = 100000102.
  3. For this exact value, store 10101 and pad it on the right with zeros to 23 bits. This is the fraction field, which IEEE 754 calls the trailing significand field. The normal exponent code tells you to restore the leading 1, giving 24 bits of precision.
Sign · bit 311
Stored exponent · bits 30 to 2310000010
Fraction field · bits 22 to 010101000000000000000000
Fields for -13.25f, shown from the most significant bit to the least.

Some sources call the full significand the mantissa. Others use mantissa for the 23 stored fraction bits. In this course, significand means the full multiplier and fraction field means the stored bits.

IEEE 754-2019 §3.4, binary fields · NVIDIA, significand terminology · Intel, stored field terminology

Join the fields and group the bits in fours to write the word in hex. A hex digit can span two fields.

1100 0001 0101 0100 0000 0000 0000 0000 → 0xc1540000

To go the other way, read the fraction field as an unsigned integer F. Its binary fraction is F / 223. For 1 ≤ B ≤ 254, the value is

(−1)s × (1 + F / 223) × 2B − 127

Here 0x540000 / 223 is 21⁄32, so the value is −(1 + 21/32) × 8 = −13.25. Adjacent fraction codes within the same normal exponent differ in magnitude by 2e − 23. Each step up in the exponent doubles that gap.

Here the value fits exactly. If a value lies between two representable numbers, the rounding rule determines which value gets stored.

Exponent codes 0 and 255 have separate decoding rules. The class table lists them for when you need them.

Intel architecture manual, Volume 1, Chapter 4, single-precision encoding

Read the bytes in C

A stored object's bytes are its object representation. On x86, the bytes of a binary32 float are little-endian, so the least significant byte has the lowest address. That means the word 0xc1540000 occupies memory in the order 00 00 54 c1.

Save this as inspect.c. The first memcpy copies the representation into a uint32_t so you can print the raw word. The second copies it into an array so you can walk the bytes in address order.

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

int main(void)
{
    const float sample = -13.25f;
    uint32_t word;
    unsigned char bytes[sizeof sample];
    _Static_assert(sizeof word == sizeof sample, "Expected a 32-bit float");

    memcpy(&word, &sample, sizeof word);
    memcpy(bytes, &sample, sizeof bytes);

    printf("word  = 0x%08" PRIx32 "\n", word);
    printf("bytes =");
    for (size_t i = 0; i < sizeof bytes; ++i) {
        printf(" %02x", (unsigned)bytes[i]);
    }
    putchar('\n');
    printf("value = %a\n", (double)sample);
}
cc -std=c11 -Wall -Wextra -pedantic inspect.c -o inspect
./inspect

On the course target, the probe prints

word  = 0xc1540000
bytes = 00 00 54 c1
value = -0x1.a8p+3

The last line writes the value in hexadecimal scientific notation. In %a output, p+3 means × 23. Each hex digit to the right of the point corresponds to four binary digits, so 1.a816 is 1.101010002. Here the float converts exactly to double, the argument type %a expects.

C11 draft §§6.2.6.1, 6.5.2.2, 7.21.6.1, and 7.24.2.1 · Intel architecture manual, Volume 1, Chapter 4, byte order for floating-point data

Why let the point move?

The gap from 1 to the next larger binary32 value is 2−23. At 1,024, the gap to the next larger value is 2−13. The adjustable step lets one type represent values across a wide range of magnitudes. For example, the BLAS routine DGEMM uses double precision for matrix multiplication. The exponent gives the arithmetic range, and the algorithm still needs a precision that meets its error budget. Reference BLAS, DGEMM

A fraction has a finite binary expansion when its denominator, in lowest terms, is a power of two. For 13.25 that denominator is 4. For 0.1 it is 10, so the binary digits repeat and the format stores a rounded value. If every input and output is measured in whole cents, the decimal fixed-point lesson shows how to keep those amounts exact.

Exercises

1. Encode and decode by hand

  1. Encode 0.15625f. Start from 5/32, write the exact binary fraction, then give the normalized significand, actual exponent, three fields, and hex word.
  2. Decode 0xc1100000. Split it into fields and recover the exact value. Write down the actual exponent separately from the stored exponent.
  3. In part b, give the full significand and the fraction as two numbers. If you lower the stored exponent by one and leave the sign and fraction bits alone, what value do you get?
Check the hand calculations

a. 5/32 = 1/8 + 1/32 = 0.001012. Normalize to 1.012 × 2−3. The sign is 0, B = 124, and the fraction starts with 01 followed by 21 zeros.

0 | 01111100 | 01000000000000000000000  →  0x3e200000

b. The sign is 1, B = 130, and F = 0x100000. So e = 3 and F / 223 = 1/8. The value is −(1 + 1/8) × 8 = −9.

c. The fraction is 1/8 and the significand is 1 + 1/8 = 9/8. Reducing B to 129 makes e = 2, so the value is −(9/8) × 4 = −4.5.

2. Extract the fields in C

In the program above, write a function static void print_fields(float value). Copy the representation into a uint32_t, then extract sign, stored exponent, and fraction with shifts and masks. Print sign and stored exponent in decimal and fraction as six hex digits. Call it with -13.25f, 0.15625f, and the value you decoded in part 1b.

Check the expected output
s=1 B=130 F=0x540000
s=0 B=124 F=0x200000
s=1 B=130 F=0x100000
Compare with one implementation
static void print_fields(float value)
{
    uint32_t word;
    memcpy(&word, &value, sizeof word);

    const unsigned sign = (unsigned)(word >> 31);
    const unsigned biased_exponent = (unsigned)((word >> 23) & UINT32_C(0xff));
    const uint32_t fraction = word & UINT32_C(0x007fffff);

    printf("s=%u B=%u F=0x%06" PRIx32 "\n", sign, biased_exponent, fraction);
}

The exponent mask removes the sign bit, which is still present after shifting the word right by 23.

Tomorrow, try decoding 0x3fc00000 from memory. Show your field split and arithmetic. Bring your answers, C code, or the first step where you got stuck to our next chat.

Check the recall exercise

s = 0, B = 127, and F = 0x400000. The value is (1 + 1/2) × 20 = 1.5.