To represent the number 6.5 in IEEE 754 single-precision (32-bit) floating point, we follow these steps:
📌 Step 1: Convert 6.5 to binary
- 6 in binary =
110
- 0.5 in binary =
.1
→ So, 6.5 =110.1
in binary
📌 Step 2: Normalize the binary number
We want it in the form of 1.xxxxx × 2^n
.
110.1
→ 1.101 × 2²
So:
- Mantissa (fraction) =
101
(rest filled with zeros) - Exponent = 2
📌 Step 3: Find the exponent bits
IEEE 754 uses a bias of 127 for single-precision:
- Exponent = 2 + 127 = 129
- 129 in binary =
10000001
📌 Step 4: Assemble the IEEE 754 components
- Sign bit = 0 (since 6.5 is positive)
- Exponent =
10000001
- Mantissa (23 bits) =
10100000000000000000000
✅ Final IEEE 754 representation of 6.5:
0 10000001 10100000000000000000000
import struct
# Convert 6.5 to IEEE 754 single-precision binary representation
num = 6.5
packed = struct.pack('>f', num) # Big-endian float (4 bytes)
binary_rep = ''.join(f'{byte:08b}' for byte in packed)
hex_rep = packed.hex().upper()binary_rep, hex_rep