IEEE 754 floating point — practice with five guided problems step by step, from decimal to hexadecimal (and the exponent I counted backwards in a mock exam)

In the first class mock exam on this topic I had to convert a negative number less than 1 to IEEE 754. I turned it into binary without trouble, normalised and, out of nerves, counted the jumps of the point as if it went left: exponent +2 instead of −2. I got a stored exponent starting with 1, the final number was worth more than 5 and I did not even look, because «the sums were right». The sums were; the direction of the point was not. Since then I always do a ten-second check: if the number is less than 1, the stored exponent has to start with 0.

This practice is about that: not about knowing the theory, which you already saw in IEEE 754 floating point, but about solving the written exam problems without losing marks on the way. There are five guided problems with the race stopwatch as the thread: a positive lap with decimals, a negative gap less than 1, five 32-bit words to read (with traps), a number that does not fit exactly and, to finish, a question in the same format as the exam one. Each problem has the idea before the maths, the step by step, the check and the doubts that usually come up.

Before you start

You should be clear about:

  • How to convert the fractional part of a decimal to binary by multiplying by 2 (from the number systems topic).
  • The three fields of single precision: 1 sign bit, 8 exponent bits in excess 127 and 23 mantissa bits.
  • What the hidden 1 is and why it is not stored.
  • The four special cases: zero, denormal, infinity and NaN.

If any of these sounds shaky, review the theory before going on.

Keep a table of powers of 2 at hand, including the negative ones: 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625. Almost every exam number has a fractional part that is the sum of a few of them.

How to solve a paper problem

The method is the same for every problem in the topic, and it is the one that lets you score even if you get one calculation wrong:

  1. Data: what you are given (decimal or hexadecimal), in which format (single precision unless they say otherwise) and what they ask for (the fields, the hexadecimal, the value).
  2. Method: the four steps (binary, normalise, fields, hexadecimal) or the same ones in reverse to read.
  3. Solve showing every step: write the binary, the normalised form with its exponent, the three fields separated by bars and the 32 bits grouped. The marker marks the working.
  4. Check: the sign of the exponent according to the size of the number, the number of bits (32) and digits (8), and a quick way back.

Problem 1: a 58.40625 s lap

Statement. The stopwatch has measured a lap of 58.40625 s. Represent it in IEEE 754 single precision and give the result in hexadecimal.

The idea

This is the most direct case: positive, greater than 1 and with an exact fractional part. You just have to apply the four steps without skipping any. The exponent will be positive, because 58 is greater than 2.

Before doing the maths, estimate: 58 is between 32 and 64, that is, between 252^5 and 262^6. The real exponent will be 5 and the stored one 5+127=1325 + 127 = 132. If you get something else later, stop.

Step by step

Step 1: binary. The integer part: 58=32+16+8+2=111010258 = 32 + 16 + 8 + 2 = 111010_2. The fractional part, multiplying by 2:

RoundCalculationDigitLeft
10.40625 · 2 = 0.812500.8125
20.8125 · 2 = 1.62510.625
30.625 · 2 = 1.2510.25
40.25 · 2 = 0.500.5
50.5 · 2 = 1.010

0 is left in the fifth round: it is exact. Read from top to bottom, 0.40625=0.0110120.40625 = 0.01101_2, and the whole number is 111010.011012111010.01101_2.

Step 2: normalise. The point has to end up right after the first 1: it jumps 5 places left.

111010.01101=1.1101001101⋅25 111010.01101 = 1.1101001101 \cdot 2^{5}

Step 3: fields.

  • Sign: positive, 0.
  • Exponent: 5+127=132=128+4=100001005 + 127 = 132 = 128 + 4 = 10000100.
  • Mantissa: what comes after the point, 1101001101, and 13 zeros up to 23 bits.

Step 4: hexadecimal. We join and group in fours from the left:

0 | 10000100 | 11010011010000000000000
0100 0010 0110 1001 1010 0000 0000 0000
  4    2    6    9    A    0    0    0
Problem 1: 58.40625 in the 32 bits58.40625 is 111010.01101, which normalised is 1.1101001101 times 2 to the 5. Sign 0, exponent 132 (10000100) and mantissa 1101001101 followed by zeros: 4269A000.58.40625 = 111010.01101 = 1.1101001101 × 2⁵01000010011010011010000000000000signexponentmantissa4269A0000 | 10000100 | 11010011010000000000000 = 4269A000₁₆
The 1 before the point does not appear in the mantissa.
58.4062510=4269A00016 58.40625_{10} = \mathrm{4269A000}_{16}

Where each hexadecimal digit comes from. Step 4 is where a bit most easily slips, because the groups of 4 ignore the fields: the first one mixes the sign with the exponent, and the third, the end of the exponent with the start of the mantissa. Group by group:

GroupBitsWhat is insideDigitWhy
10100The sign (0) and the first three bits of the exponent (100)40100 = 4
20010Bits 4 to 7 of the exponent20010 = 2
30110The last bit of the exponent (0) and the first three of the mantissa (110)64 + 2 = 6
41001Bits 4 to 7 of the mantissa98 + 1 = 9
51010The last three useful bits of the mantissa (101) and the first padding zeroA8 + 2 = 10, which is A in hexadecimal
6 to 80000Only padding zeros0Nothing to add

Look at group 5: the padding zeros go on the right and they start inside it. If you had put them to the left of 1101001101, that group and every group after it would change.

Before grouping, write the 32 bits in one row, without the lines between the fields. The lines help you see the sign, the exponent and the mantissa, but when you count in fours they get in the way: the last bit of the exponent ends up in the same group as the first three of the mantissa.

Check

  • Sign of the exponent: 58 > 2, positive exponent, and the stored one starts with 1 (10000100). Good.
  • Bits and digits: 1 + 8 + 23 = 32 bits, 8 hexadecimal digits.
  • Way back: 4269A000 → mantissa 1.1101001101, exponent 132−127=5132 – 127 = 5 → 111010.01101=58+0.25+0.125+0.03125=58.40625111010.01101 = 58 + 0.25 + 0.125 + 0.03125 = 58.40625. It matches.

Questions you might ask

Why is the first hexadecimal digit 4 if the sign is 0? Because the first group of 4 bits is 0100: the sign (0) and the first three bits of the exponent (100). The groups ignore the fields; almost every positive number with a positive exponent starts with 4 (or with 3 if the number is less than 2).

How many mantissa bits have I really used? 10 out of 23. The other 13 are padding zeros, and they go on the right because they are digits after the point: 1.1101001101=1.11010011010000…1.1101001101 = 1.11010011010000\ldots

What if the fractional part did not reach 0 in the table? Then the number is not exact: you have to carry on until you have 24 significant bits and round. We do that in problem 4.

Can I do the integer part by subtracting powers of 2 instead of dividing? Yes, and it is usually faster: 58 − 32 = 26, 26 − 16 = 10, 10 − 8 = 2, 2 − 2 = 0. The powers used (32, 16, 8, 2) are the ones.

Writing the mantissa as 11101001101 (with the leading 1). That 1 is the hidden one: if you put it in, the whole mantissa shifts one bit and the number is no longer 58.40625.

Try it yourself

  • Convert a lap of 47.8125 s to hexadecimal (you should get 423F4000).
  • Convert 19.3125 and check that the stored exponent is 10000011, because 19 is between 16 and 32.

Problem 2: a gap of −0.34375 s

Statement. The runner has beaten the record by 0.34375 s and the stopwatch stores the gap as −0.34375. Represent it in IEEE 754 single precision and in hexadecimal.

The idea

Two new things at once: the negative sign, which only changes the first bit, and a number less than 1, which makes the point jump right and the exponent negative. Everything else is done with the absolute value, 0.34375.

For a number between 0.25 and 0.5 the real exponent is −2: 0.25=2−20.25 = 2^{-2}. Before you start you already know the stored exponent will be −2+127=125-2 + 127 = 125 and that it will start with 0.

Step by step

Step 1: binary. There is no integer part. The fractional part:

RoundCalculationDigitLeft
10.34375 · 2 = 0.687500.6875
20.6875 · 2 = 1.37510.375
30.375 · 2 = 0.7500.75
40.75 · 2 = 1.510.5
50.5 · 2 = 1.010

So 0.34375=0.0101120.34375 = 0.01011_2.

Step 2: normalise. The first 1 is in the second position after the point; the point jumps 2 places right:

0.01011=1.011⋅2−2 0.01011 = 1.011 \cdot 2^{-2}

Step 3: fields.

  • Sign: negative, 1.
  • Exponent: −2+127=125=64+32+16+8+4+1=01111101-2 + 127 = 125 = 64 + 32 + 16 + 8 + 4 + 1 = 01111101.
  • Mantissa: 011 and 20 zeros.

Step 4: hexadecimal.

1 | 01111101 | 01100000000000000000000
1011 1110 1011 0000 0000 0000 0000 0000
  B    E    B    0    0    0    0    0
Problem 2: −0.34375, negative and less than 10.34375 is 0.01011, which normalised is 1.011 times 2 to the −2. Sign 1, exponent −2 + 127 = 125 (01111101) and mantissa 011 followed by zeros: BEB00000.−0.34375 = −0.01011 = −1.011 × 2⁻²10111110101100000000000000000000signexponentmantissaBEB000001 | 01111101 | 01100000000000000000000 = BEB00000₁₆
Negative exponent: the stored exponent starts with 0.
−0.3437510=BEB0000016 -0.34375_{10} = \mathrm{BEB00000}_{16}

Check

  • Sign of the exponent: the number is less than 1, the real exponent is negative and the stored one starts with 0 (01111101). Good.
  • Sign of the number: the first bit is 1 and the first hexadecimal digit is B (1011), 8 or more, as in every negative number.
  • Way back: −1.011⋅2−2=−0.01011=−(0.25+0.0625+0.03125)=−0.34375-1.011 \cdot 2^{-2} = -0.01011 = -(0.25 + 0.0625 + 0.03125) = -0.34375.

Questions you might ask

Does the negative sign change anything other than the first bit? No. Unlike two’s complement, in IEEE 754 the sign is separate (it is sign-magnitude): 0.34375 would be 3EB00000 and −0.34375 is BEB00000. Only the first hexadecimal digit changes, from 3 to B, because 2312^{31} is added.

Why 125 and not 129? Because the real exponent is −2 and −2+127-2 + 127 is stored. If you get 129 you have added 2: that is the mistake in my story, counting the jumps as if the point went left.

What happens to the zeros before the first 1? They disappear when normalising: they are inside the exponent. 0.01011 and 1.011 · 2⁻² are the same number, and the mantissa starts at the bit after the first 1.

How do I quickly know whether the stored exponent starts with 0 or 1? Numbers from 2 upwards: with 1. Numbers between 1 and 2: 01111111 (exponent 0). Numbers less than 1: with 0.

Storing exponent −2 in two’s complement (11111110). In IEEE 754 the exponent always goes in excess 127: 11111110 would be exponent 127, a huge number.

What if you counted the jumps of the point backwards, like I did in my mock exam?

That is exactly what happened to me: normalising 0.01011 as 1.011 with exponent +2 instead of −2. The stored exponent would be 2 + 127 = 129 = 10000001, and nothing else would change: sign 1 and mantissa 011 with 20 zeros.

Putting the fields together gives 1100 0000 1011 0000 0000 0000 0000 0000, that is, C0B00000. Now read it back: 1.011 with the point two places to the right is 101.1 = 4 + 1 + 0.5 = 5.5. You would have stored −5.5 instead of −0.34375, sixteen times as much: the exponent has moved 4 places (from −2 to +2) and each place doubles the number.

The arithmetic is all correct; what fails is the direction of the point, and that is why going over the sums does not catch it. The two ten-second checks do: the stored exponent starts with 1 for a number less than 1, and reading it back gives 5.5.

Try it yourself

  • Convert −0.15625 (the gap from the theory): you should get BE200000.
  • Convert positive 0.34375 and check that only the first digit changes.

Problem 3: five words from the stopwatch

Statement. When the stopwatch memory is downloaded, these five 32-bit words in IEEE 754 single precision come out. Say what each one represents and, if it is a number, its decimal value: 42BF6000, 80000000, 7F800000, FFC00000 and 00200000.

The idea

Reading is easy if you follow an order: convert to binary, split the fields and, before applying the formula, look at the exponent. If it is all zeros or all ones, it is a special case and the normal formula does not apply.

With a bit of practice the special cases show in the hexadecimal. If it starts with 7F or FF and the third digit is 8 or more, the exponent is 11111111: infinity if everything else is 0, NaN otherwise. If it starts with 00 or 80 and the third digit is less than 8, the exponent is 00000000: zero or denormal.

Step by step

42BF6000. In binary, 0100 0010 1011 1111 0110 0000 0000 0000:

  • Sign 0; exponent 10000101 = 133, normal; real exponent 133−127=6133 – 127 = 6.
  • Mantissa 0111111011 (and zeros); with the hidden 1, 1.0111111011.
  • Value: 1.0111111011⋅26=1011111.1011=95+0.5+0.125+0.0625=95.68751.0111111011 \cdot 2^{6} = 1011111.1011 = 95 + 0.5 + 0.125 + 0.0625 = 95.6875.

The first word, field by field. The 32 bits are cut 1 + 8 + 23, and those cuts do not match the hexadecimal digits:

FieldBitsValueWhy
Sign0PositiveIt is the first bit of the 4 (0100)
Exponent10000101133The last three bits of the 4 (100), the whole 2 (0010) and the first bit of the B (1011): 128 + 4 + 1 = 133
Case—NormalisedThe exponent is neither 00000000 nor 11111111
Real exponent—6133 − 127 = 6
Mantissa0111111011 and zeros1.0111111011The last three bits of the B (011), the F (1111) and the 011 of the 6; the hidden 1 goes in front because it is normalised
Value—95.6875The point moves 6 places to the right: 1011111.1011

80000000. Sign 1, exponent 00000000 and mantissa 0: it is negative zero, −0.

7F800000. 0111 1111 1000 0000…: sign 0, exponent 11111111 and mantissa 0: plus infinity. The stopwatch would have stored a time that went out of range here.

FFC00000. 1111 1111 1100 0000…: sign 1, exponent 11111111 and mantissa 100…, not zero: NaN. For example, the result of an average of 0 laps, 0/0.

00200000. 0000 0000 0010 0000…: sign 0, exponent 00000000 and mantissa 01000…, not zero: denormal. Its value uses 0. instead of 1. and the fixed exponent −126:

0.01⋅2−126=2−2⋅2−126=2−128≈2.94⋅10−39 0.01 \cdot 2^{-126} = 2^{-2} \cdot 2^{-126} = 2^{-128} \approx 2.94 \cdot 10^{-39}
Problem 3: five words, five cases42BF6000 is a normal number worth 95.6875; 80000000 is negative zero; 7F800000, plus infinity; FFC00000, NaN; and 00200000, a denormal worth 2 to the −128.hexexponentmantissacasevalue42BF6000100001010111111011…normal+95.68758000000000000000000…0zero−07F80000011111111000…0infinity+∞FFC0000011111111100…0NaNNaN0020000000000000010…0denormal2⁻¹²⁸
The exponent decides the case; the mantissa, which of the two.

Where 00200000 is cut. It is the most misleading word, because its only 1 is in the third hexadecimal digit and it looks as if it belonged to the exponent:

PieceBitsWhy
Sign0It is the first bit of the first 0 (0000)
Exponent00000000The three bits left in the first 0, the four of the second and the first bit of the 2 (0010), which is also 0
Mantissa010 and zerosThe last three bits of the 2 (010) and everything else zero
CaseDenormalisedExponent all zeros and mantissa other than zero
Mantissa as read0.01With 0 in front, not 1, because it is denormalised

So the 1 of the 2 is the second bit of the mantissa: 0.01 in binary is 0.25, and that is where the factor in the calculation above comes from.

Check

  • In all five words the first thing we looked at was the exponent: only the first one is a normal number.
  • In the denormal we used 2−1262^{-126}, not 20−1272^{0 – 127}, and the mantissa with 0. in front. With the normal formula we would have got 1.01⋅2−1271.01 \cdot 2^{-127}, a number two and a half times bigger.
  • The value of 42BF6000 checks out: 95.6875=1011111.1011295.6875 = 1011111.1011_2, exponent 6, mantissa 0111111011.

Questions you might ask

Why does −0 exist? Because the sign is separate: with everything at zero except the sign you get −0. For comparisons −0 and +0 are equal; the difference only shows in rare cases, like 1/(−0)=−∞1/(-0) = -\infty.

Is there only one NaN? No: any non-zero mantissa with exponent 11111111 is NaN. There are more than eight million positive NaNs.

Why does the denormal use −126 and not −127? So that there is no gap between the smallest normal, 1.0⋅2−1261.0 \cdot 2^{-126}, and the largest denormal, 0.111…1⋅2−1260.111\ldots1 \cdot 2^{-126}. Both use the same 2−1262^{-126} and the numbers follow on without a gap.

What if I am given the 32 bits in binary instead of hexadecimal? Even better: you skip the first step. Split them 1 + 8 + 23 and carry on.

Applying the formula to 80000000 or 00200000 without looking at the exponent. Every exponent 00000000 is special: zero if the mantissa is 0, denormal otherwise.

Try it yourself

  • Read FF800000 and 00800000 (hints: one is special and the other is the smallest normal, 2−1262^{-126}).
  • Read C1A00000: you should get −20.

Problem 4: the tenth that does not fit

Statement. The stopwatch measures in tenths of a second. Represent 0.1 in IEEE 754 single precision, say whether it is stored exactly and, if not, what value it stores and how far off it is. Then compare it with double precision.

The idea

0.1 is recurring in binary: the table of multiplications by 2 never ends. We will have to work out just enough bits (24 significant ones, counting the hidden one), look at what is left over and round to nearest.

You do not need 40 multiplications: as soon as you see a remainder come back, the digits repeat with it.

Step by step

Step 1: binary. Multiplying by 2: 0.2 → 0; 0.4 → 0; 0.8 → 0; 1.6 → 1; 1.2 → 1; 0.4 → 0; 0.8 → 0; 1.6 → 1… 0.4 has come back in the sixth round, so from there 0011 repeats:

0.110=0.0 0011 0011 0011…2 0.1_{10} = 0.0\,0011\,0011\,0011\ldots_2

Step 2: normalise. The first 1 is in the fourth position: the point jumps 4 places right.

0.1=1.1001 1001 1001 1001 1001 100 ∣ 11001…⋅2−4 0.1 = 1.1001\,1001\,1001\,1001\,1001\,100\,|\,11001\ldots \cdot 2^{-4}

The bar marks where the 23 mantissa bits end.

Step 3: round. What is left over starts with 11: it is more than half of the last bit that is stored. The standard’s rule (round to nearest) says:

What is left overWhat you do
starts with 0cut it off (truncate)
starts with 1 and there is some 1 after itadd 1 to the last bit
is exactly 1000…tie: choose the one ending in 0 (the «even» one)

Here we add 1:

Problem 4: where 0.1 is cut and which way it roundsThe mantissa of 0.1 is 1001 1001 1001… forever. 23 bits fit; the rest starts with 11, more than half of the last bit, so 1 is added and the mantissa ends in 1101. The float is 0.100000001490…; the double, 0.1000000000000000055….0.1 = 1.10011001100110011001100|11… × 2⁻⁴1001100110011001100110011the 23 bits that fitthe rest: 11…the rest is more than half of the last bit → it rounds up:…1001100 + 1 = …1001101single: 3DCCCCCD = 0.100000001490… double: 3FB999999999999A = 0.1000000000000000055…
Round to nearest: if the rest is more than half, round up.
cut mantissa:        10011001100110011001100
+ 1 (rounding):                            1
stored mantissa:     10011001100110011001101

Step 4: fields and hexadecimal. Sign 0; exponent −4+127=123=01111011-4 + 127 = 123 = 01111011; the rounded mantissa:

0 | 01111011 | 10011001100110011001101
0011 1101 1100 1100 1100 1100 1100 1101
  3    D    C    C    C    C    C    D
0.110≈3DCCCCCD16=0.100000001490116… 0.1_{10} \approx \mathrm{3DCCCCCD}_{16} = 0.100000001490116\ldots

It is not exact: the float stores slightly more than 0.1, with an error of about 1.5⋅10−91.5 \cdot 10^{-9}.

In double precision the exponent has 11 bits (excess 1023: −4+1023=1019=01111111011-4 + 1023 = 1019 = 01111111011) and the mantissa 52. The cut falls somewhere else in the 1001 1001…, what is left over again starts with 1 and it is also rounded up: 3FB999999999999A, which is 0.1000000000000000055… The error drops to about 5.5⋅10−185.5 \cdot 10^{-18}, but it does not go away.

What if you cut the mantissa without rounding?

The mantissa would stay as 10011001100110011001100, which ends in 1100: the last hexadecimal digit would be C and not D, and the stored number would be 3DCCCCCC.

That number is 0.0999999940395…, a little less than 0.1. Its distance from 0.1 is 0.0000000059604…, four times that of the rounded number, which is 0.0000000014901…

The four times have an explanation. What is left over, 11001100…, measured in units of the last bit that is stored, is worth 0.8 of that bit. If you cut, you lose that 0.8; if you round up, you overshoot by only 0.2. That is why round to nearest is never off by more than half a bit, while cutting can be off by almost a whole one.

Check

  • The stored exponent starts with 0 (01111011): the number is less than 1. Good.
  • The stored mantissa has 23 bits and ends in 1101: the rounding up shows in the last digit, D (1101) instead of C (1100).
  • The stored value, 0.100000001490…, is greater than 0.1: it matches rounding up.

Questions you might ask

Why is the mantissa 23 bits if the precision is 24? Bit 24 is the hidden 1 before the point: it counts, but it takes no space.

Is it always rounded up? No: it depends on what is left over. In 0.1 the leftover is 11…, more than half. If it were 01…, it would be cut and the float would store slightly less.

What happens when a double is stored in a float? It is rounded again to 24 significant bits. If the number fits in single precision, like 17.25 (double 4031400000000000, single 418A0000), nothing is lost; if it does not fit exactly, like 0.1, precision is lost; and if it is larger than the single-precision maximum (about 3.4⋅10383.4 \cdot 10^{38}), it becomes infinity.

Can the exam ask for a non-exact number? Yes, class exercises have lots of them. What matters is the method: work out enough to have 24 significant bits plus one or two more to decide the rounding, and say the result is approximate.

Padding a recurring number with zeros (cutting without looking at the leftover) or carrying on producing digits past bit 23 of the mantissa. Work out the 23, look at what comes next and decide.

Try it yourself

  • Represent 0.2 (hint: it is 0.1 · 2, so only the exponent changes): 3E4CCCCD.
  • Is 0.625 exact? Do the table and check it before turning it into 3F200000.

Problem 5: the exam-style question

Statement. This one has the shape of the question in the written theory exam: a number, the format, the answer in a box and its marks.

Question. Convert the number 37.68751037.6875_{10} to IEEE 754 single-precision floating point with a hidden 1 and write the result in hexadecimal. [0.75 marks] Answer box: «Result in hexadecimal: ________».

The idea

It is problem 1 with the rush of the exam. The box only asks for the hexadecimal, but the working goes on the sheet and it is what saves marks if the last calculation goes wrong. Write the four steps even if they look obvious.

37 is between 32 and 64: the real exponent will be 5 and the stored one 132 = 10000100, the same as in problem 1. If you get a different one in the exam, check the normalisation.

Step by step

Step 1. 37=32+4+1=100101237 = 32 + 4 + 1 = 100101_2. The fractional part:

RoundCalculationDigitLeft
10.6875 · 2 = 1.37510.375
20.375 · 2 = 0.7500.75
30.75 · 2 = 1.510.5
40.5 · 2 = 1.010

37.6875=100101.1011237.6875 = 100101.1011_2.

Step 2. 100101.1011=1.001011011⋅25100101.1011 = 1.001011011 \cdot 2^{5}.

Step 3. Sign 0; exponent 5+127=132=100001005 + 127 = 132 = 10000100; mantissa 001011011 and 14 zeros.

Dropping the leading zeros of the mantissa, as if they were zeros to the left of a whole number. Here they are digits after the point: 1.001011011 is not 1.1011011. If you store the mantissa as 1011011, the result comes out as 425B0000, which is 54.75 and not 37.6875. The only zeros you add are the padding ones, at the end: 23 − 9 = 14, because 001011011 has 9 bits.

Step 4.

0 | 10000100 | 00101101100000000000000
0100 0010 0001 0110 1100 0000 0000 0000
  4    2    1    6    C    0    0    0
Problem 5: the exam-style question, 37.687537.6875 is 100101.1011, which normalised is 1.001011011 times 2 to the 5. Sign 0, exponent 132 (10000100) and mantissa 001011011 followed by zeros: 4216C000.37.6875 = 100101.1011 = 1.001011011 × 2⁵01000010000101101100000000000000signexponentmantissa4216C0000 | 10000100 | 00101101100000000000000 = 4216C000₁₆
What goes in the answer box: the 8 hexadecimal digits.

Result in hexadecimal: 4216C000

Check and how it is marked

Way back: 4216C000 → sign 0, exponent 132−127=5132 – 127 = 5, mantissa 1.001011011 → 100101.1011=37+0.5+0.125+0.0625=37.6875100101.1011 = 37 + 0.5 + 0.125 + 0.0625 = 37.6875. It matches.

A reasonable split of the 0.75 marks (it is a rough guide, mine, not official: each lecturer marks their own way) would be:

What can be seen on the sheetMarks
37.6875 converted to binary, with the multiplications of the fractional part0.25
Normalisation 1.001011011⋅251.001011011 \cdot 2^5 and the three fields (sign, 10000100, mantissa)0.25
The 32 bits grouped and the hexadecimal 4216C000 in the box0.25

With this split, a slip when grouping into hexadecimal leaves you with half a mark; a box with the right number and no working, depending on the marker, may be worth little.

Questions you might ask

Do I have to write the 32 bits in binary or is the hexadecimal enough? Write them: it is how the marker sees where each digit comes from.

What if they ask for «with a hidden 1» and I store it? Then the number is wrong even though the method looks fine: that is exactly what the statement reminds you of. Without the hidden 1, the mantissa would start with 1 and everything would shift.

Do I write 4216C000, 0x4216C000 or 4216C000₁₆? Any of the three is understood; what cannot be missing are the 8 digits.

Normalising to 0.1001011011⋅260.1001011011 \cdot 2^{6} (the fractional-mantissa form of the notes) when they ask for IEEE 754 with a hidden 1. In IEEE 754 the form is 1.…1.\ldots and the exponent 5.

Try it yourself

  • Do the same question with −9.40625. The solution is in the «Check it with Python» below, so you do not see it too early.

Common mistakes in this practice

  • Counting the jumps of the point backwards. Numbers less than 1: negative exponent and a stored exponent starting with 0.
  • Putting the hidden 1 into the mantissa. The mantissa starts at the bit after the point.
  • Padding the mantissa with zeros on the left. The padding zeros go on the right.
  • Grouping into hexadecimal field by field. The 32 bits are joined and grouped in fours from the left.
  • Applying the normal formula to an exponent 00000000 or 11111111. Check first whether it is a special case.
  • Cutting a recurring number without rounding. Look at the first leftover bit (and what comes after) before deciding.

Check it with Python

This script repeats every calculation of the practice, once: it converts each number to binary with the same multiplications by 2 as the tables (with exact fractions, so that no rounding sneaks in), works out the fields with struct and reads the five words of problem 3.

# Problems of the floating-point practice: every calculation, once
import struct
from fractions import Fraction

def to_binary(x, maxfrac=30):
    """Integer part by divisions and fractional part by multiplying by 2."""
    whole, frac = int(abs(x)), Fraction(abs(x)) - int(abs(x))
    digits = ''
    while frac and len(digits) < maxfrac:             # each round: x2 and keep the integer part
        frac *= 2
        digits += str(int(frac)); frac -= int(frac)
    return format(whole, 'b') + '.' + (digits or '0')

def ieee(x):
    w = struct.unpack('>I', struct.pack('>f', x))[0]
    b = format(w, '032b')
    return b[0], b[1:9], b[9:], f'{w:08X}'

def show(x):
    s, e, m = ieee(x)[:3]
    print(f'  {x}: binary {to_binary(x)}  exponent {int(e, 2) - 127} + 127 = {int(e, 2)} = {e}')
    print(f'  {s} | {e} | {m} = {ieee(x)[3]}')

print('== Problem 1'); show(58.40625)
print('== Problem 2'); show(-0.34375)
print('== Problem 3')
for h in ('42BF6000', '80000000', '7F800000', 'FFC00000', '00200000'):
    x = struct.unpack('>f', bytes.fromhex(h))[0]
    b = format(int(h, 16), '032b')
    print(f'  {h}: s={b[0]} e={b[1:9]} m={b[9:12]}... -> {x}')
print('  2^-128 =', 2 ** -128)
print('== Problem 4')
print('  0.1 in binary:', to_binary(0.1, 28) + '...')
print('  single:', ieee(0.1)[3], '=', f'{struct.unpack(">f", struct.pack(">f", 0.1))[0]:.20f}')
print('  double:', struct.pack('>d', 0.1).hex().upper(), '=', f'{0.1:.20f}')
print('== Problem 5: 37.6875'); show(37.6875)
print('== Try it yourself: -9.40625'); show(-9.40625)
== Problem 1
  58.40625: binary 111010.01101  exponent 5 + 127 = 132 = 10000100
  0 | 10000100 | 11010011010000000000000 = 4269A000
== Problem 2
  -0.34375: binary 0.01011  exponent -2 + 127 = 125 = 01111101
  1 | 01111101 | 01100000000000000000000 = BEB00000
== Problem 3
  42BF6000: s=0 e=10000101 m=011... -> 95.6875
  80000000: s=1 e=00000000 m=000... -> -0.0
  7F800000: s=0 e=11111111 m=000... -> inf
  FFC00000: s=1 e=11111111 m=100... -> nan
  00200000: s=0 e=00000000 m=010... -> 2.938735877055719e-39
  2^-128 = 2.938735877055719e-39
== Problem 4
  0.1 in binary: 0.0001100110011001100110011001...
  single: 3DCCCCCD = 0.10000000149011611938
  double: 3FB999999999999A = 0.10000000000000000555
== Problem 5: 37.6875
  37.6875: binary 100101.1011  exponent 5 + 127 = 132 = 10000100
  0 | 10000100 | 00101101100000000000000 = 4216C000
== Try it yourself: -9.40625
  -9.40625: binary 1001.01101  exponent 3 + 127 = 130 = 10000010
  1 | 10000010 | 00101101000000000000000 = C1168000

Notice three things: to_binary produces the same digits as the tables; in problem 3 Python writes -0.0, inf and nan, just like our conclusions; and in problem 4 the float of 0.1 moves away from the real value at the ninth decimal digit and the double at the eighteenth. The last line is the solution to the «Try it yourself» of problem 5: C1168000.

Summary

  • Every problem in this topic is solved with four steps: binary, normalise, fields and hexadecimal (or in reverse to read).
  • The exponent is stored by adding 127; numbers less than 1 have a stored exponent starting with 0.
  • The sign is separate: changing it only changes the first bit (the first hexadecimal digit goes from 3 or 4 to B or C).
  • When reading, look at the exponent first: 00000000 and 11111111 are special cases.
  • A recurring number is worked out up to the 23 mantissa bits and rounded to nearest by looking at the leftover.
  • In the exam, the working on the sheet is worth as much as the box.

Checklist: have you practised it?

  • Have you done the five problems with pen and paper before looking at the step by step?
  • Have you checked every result with the way back?
  • Can you recognise the special cases just by looking at the hexadecimal?
  • Can you decide whether to round up or cut?
  • Have you run the Python script and checked its outputs match your calculations?
  • Have you tried the «Try it yourself» tasks without looking at the theory?

If you answered yes to almost everything, you are ready for the floating-point exercises, where the steps are no longer done for you. And if any of them was hard, the IEEE 754 floating-point theory explains it from the start. See you there!

Next step: exercisesIEEE 754 floating-point exercises — laps, gaps to the record and an old 16-bit stopwatch (and the 3.25 that turned into 2.0000012)

Did this article help you? Here is how you can support SergioLearns →