Finding 32 bits value with binary multiplication

150 Views Asked by At

I currently am studying 32 bits conversions at shcool. I am a bit at a loss, tho, as to why I should either move a floating point when I am done with converting everything to 32 bits.


In: 21.5 * 12.75

The answer, in base ten is: 274.125

The answer I found in 32 bits is: 0 | 10000110 | 00100100100000000000000|

Exponant being: 7 Mantissa being: 137.0625

I now that to achieve the correct answer I have to move one bit to the right, but I fail to understand why and it bugs me...

Should I add one bit to the exponant? Should I move 1 bit to the right of the floating point? That'd give me a 0 as a sign bit which would make sense in a way, but I still need that 1,xxxxx for scientific notation... Argh... Help.

The maths I did in written format

2

There are 2 best solutions below

1
On BEST ANSWER

Your first mantissa is $1.01011_2$ and the second is $1.10011_2$. When you multiply those two numbers, you get $10.0010010001_2$. (The product of two numbers between $1$ and $2$ may be anywhere between $1$ and $4$!) You can detect that by counting the number of binary places (I was tempted to write "decimal" places - but this is binary after all!): after counting $5+5=10$ binary places, you have "$10.$" or "$11.$" (rather than just "$1.$") before the binary point.

This is why you need to "fix up" the mantissa by dividing by two/right shifting: $1.00010010001_2$, and then you need to match that by incrementing your exponent by $1$ (from $7$ to $8$), eventually achieving:

$$0|10000111|00010010001000000000000$$

If the original product of the mantissas ended up as $1.\text{<10 digits>}_2$, then it would mean that it did not need fixing up, and your work would be correct.

0
On

By IEEE 754 convention, the floating format consists of one sign bit, 8 bits for the exponent, and 23 bits for the mantissa (with a leading $1$ understood, but not stored). To multiply two IEEE floats, the sign is the easiest part: The sign of the product is simply the XOR of the factor signs. Next, let us deal with the mantissas:

  • take only the lowest 31 bits and prepend a $1$ (the "hidden bit"), giving a 24 bit (or nicely 3 bytes) string that we interpret as an integer in the range $2^{23},\ldots, 2^{24}-1$
  • multiply these to produce a 48 bit (or nicely 6 bytes) integer in the range $2^{46},\ldots,2^{48}-2^{25}+1$. That is, the highest of the 48 bits may be $1$ or $0$ (but then the next highest must be $1$).
  • If the highest bit is $0$, we shift our result left by one and remember this fact in order to adjust the exponent later. So now the highest bit is definitely $1$.
  • Take only the highest three byte, drop the leading $1$, and here you have the mantissa of the result.

Remains the exponent. By its nature, the exponent of the product is essentially the sum of the factor exponents, but we have to adjust it due to the so-called bias of the exponent (which is a property of the IEEE 754 format) as well as whether or not we had to do the shifting in step 3 of the mantissa computation!

Perhaps it is easiest if we try to find out how to make multiplication by $1$, which is represented as $$ 1=2^0\cdot(1+0)=0|\underbrace{01111111}_{127}|000\ldots0,$$ work as expected (i.e., the product is simply the other factor). Note that $$ 0|\underbrace{01111110}_{126}|111\ldots1$$ is just a tiny bit smaller so should produce nearly the same results under multiplication.

If we multiply $$ s|\underbrace{eeeeeeee}_E|aaa\ldots a$$ with $1$ as described above, the sign of the result is of course $s$ again. Next, for the mantissa we first compute $$10000000\,00000000\,00000000\cdot 1aaaaaaa\,aaaaaaaa\,aaaaaaaa\\ = 01aaaaaa\,aaaaaaaa\,aaaaaaaa\,a0000000\,00000000\,00000000 $$ For the exponent, we remember that we have to shift left before chopping off three bytes and dropping the leading bit. The final exponent is $E+127-(\text{correction in case of shift required in mantissa})$. As we clearly want $E$, the correction in case of shift required in mantissa must be $127$.

Now what if we compute with the almost-one shown above instead? Sign is clear, but for the mantissa we compute $$11111111\,11111111\,11111111\cdot 1aaaaaaa\,aaaaaaaa\,aaaaaaaa\\ \approx 2^{24}\cdot 1aaaaaaa\,aaaaaaaa\,aaaaaaaa\\ = 1aaaaaaa\,aaaaaaaa\,aaaaaaaa\,00\ldots$$ so this time there is no shift needed to obtain the original mantissa back (approximately, and I ignore some borderline cases). The final exponent is $E+126-(\text{correction in case of no shift required})$. To make things work, the correction in case of no shift required in mantissa must be $126$.


In the problem, you multiply $$ 21.5 = 0|\underbrace{10000011}_{131}|0101100\ldots0$$ and $$ 12.75 = 0|\underbrace{10000010}_{130}|1001100\ldots0.$$ As $$ 10101100\,00\ldots \cdot 11001100\,00\ldots = 10001001\, 00010000\,0\ldots,$$ we are in the no-mantissa-shift-needed case, so that the exponent is $131+130-126=135$ and ultimately the IEEE product is $$0|10000111|0001001000100\ldots $$ We read this as $$2^{135-127}\cdot\frac{1000 1001 0001_2}{1000 0000 0000_2} =2^8\cdot \frac{2193}{2^{11}}=274.125$$