1
$\begingroup$

I'm trying to add two 16 bit numbers that use a similar format as IEEE 754. The format is in the image below:

16 Bit Number Format

I can't figure out how to add together two numbers for the life of me.

Specifically, 0x82A3 + 0x0B98. In my attempt to solve this, I got 0x0B98 as the final sum, but that doesn't seem right to me.

$\endgroup$
  • $\begingroup$ $3.3\times 10^7-7.4\times 10^2=3.3\times 10^7$ to two significant figures. $\endgroup$ – Derek Elkins May 5 '18 at 8:52
  • $\begingroup$ If this format uses IEEE's hidden leading "1.", I get 0xB97 (or 0B98 using round towards +infinite). Did you place this leading "1." ? Did you make a subtraction (because opposite signs) ?. $\endgroup$ – TEMLIB May 5 '18 at 12:25
  • $\begingroup$ There are plenty of places that define how to add these numbers. It's hard to see any point in somebody here writing yet another description. So what's your actual problem? $\endgroup$ – David Richerby May 9 '18 at 16:42
2
$\begingroup$

It is difficult, and there are lots of special cases to handle.

Step 1: Determine if any of the operands is an Infinity or a Not-A-Number. In this case, look up the IEEE 754 standard and determine the result accordingly.

Step 2: Extract the sign, the biased exponent, and the mantissa. Add any implicit leading bit to the mantissa. Check whatever special rules there are: In the IEEE 754 32 and 64 bit format, a biased exponent of 0 means that there is no implicit leading bit in the mantissa, but the exponent should be treated as if it was 1. Add three extra bits containing zeroes to the mantissa.

Step 3: Make sure the exponents are the same. If one number has an exponent that is smaller by k, then shift that numbers mantissa to the right by k bit positions. If k is greater than 3, then set the last bit of the mantissa to 1 if any non-zero bits were shifted out. If k is so large that all mantissa bits would be shifted out then set the mantissa to zero, and don't try to shift by some huge amount.

Step 4: If the operands have different signs and the second mantissa is larger then exchange the operands. Add or subtract the mantissas, depending on the sign bits.

Step 5: If you added the mantissas and there was a carry, then increase the exponent, and shift the mantissa one bit position to the right. If the bit that is shifted out is non-zero, then set the last bit of the mantissa.

Step 6: If you subtracted the mantissas then count the number of leading zero bits in the mantissa. If all mantissa bits are zero then set the biased exponent to 1. Otherwise, shift the mantissa to the left and decrease the exponent, making sure that the biased exponent doesn't get smaller than 1.

Step 7: Perform rounding according to IEEE 754 rules. That's where the three extra mantissa bits are used. Remove the extra 3 mantissa bits. Due to the "round to even" rule, you round up (increase the mantissa by 1) if the last bit is 0 and the bits removed are 101 or larger, or the last bit is 1 and the bits removed are 100 or larger.

Step 8: If you rounded up and there is a carry then shift the mantissa one bit to the right and increase the exponent. If the biased exponent is too large then the result is infinity. If the biased exponent is 1 and the highest mantissa bit is zero then the biased exponent is changed to 0.

Step 9: Drop the highest bit of the mantissa, and pack sign, biased exponent, and mantissa together.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.