159. Float to Bytes Type Punning

Your task is to define a union that allows viewing a 32-bit floating-point value both as a float and as its raw byte representation.

The union must contain:

  • float f
  • uint8_t bytes[4]

The program will:

  1. Read a floating-point value from input
  2. Store it in the union
  3. Print the four raw bytes in memory order, as two-digit uppercase hexadecimal values separated by spaces

Input / Output Specification

Input

  • A single 32-bit floating-point number in decimal notation

Output

  • Four bytes printed in little-endian memory order
  • Each byte printed as:
    • Two-digit
    • Uppercase hexadecimal
    • Followed by a single space

Output format example:

3F 80 00 00 

(Note: A trailing space after the last byte is required.)

Example 1

Input:

1.0

Output:

00 00 80 3F 

Example 2

Input:

-2.5 

Output:

00 00 20 C0 

⚠️ Embedded System Assumptions (Mandatory Constraints):

  • The target platform uses:
    • IEEE-754 single-precision (32-bit) floats
    • Little-endian byte order
  • The compiler supports union-based type punning, as is commonly allowed in embedded GCC/Clang toolchains.
  • This problem is intended for embedded/firmware environments, not strictly portable ISO C++.

 

 

 

 

Loading...

Input

1

Expected Output

00 00 80 3F