Simvik

Bits of a float

Return the IEEE 754 bit pattern of a float. An easy embedded C problem (Structs, unions and memory layout), run and graded in the browser.

easy · Structs, unions and memory layout · unions · type-punning

To send a float over a bus or store it in EEPROM you need its raw 32 bits, not its value converted to an integer.

Return the bit pattern of f. For example 1.0f is 0x3F800000.

Starter code

uint32_t float_bits(float f) {
  // A cast converts the VALUE: (uint32_t)1.0f is 1, not 0x3F800000.
  return (uint32_t)f;
}

Example tests

One

printf("0x%08X\n", float_bits(1.0f));

Expected output:

0x3F800000

Minus two

printf("0x%08X\n", float_bits(-2.0f));

Expected output:

0xC0000000

Learn it first

More Structs, unions and memory layout problems