Reputation: 39
I have to add these two IEEE754 numbers:
-0,375 = 1 0111 1101 100 0000 0000 0000 0000 0000 //
8,5 = 0 1000 0010 000 1000 0000 0000 0000 0000
I can add two positive numbers, but if one is negative I don't know how to do it! And i'm not allowed to subtract, it has to be an addition!!
Upvotes: 1
Views: 5052
Reputation: 1447
Sign bit: 1 -> negative.
Exponent: 01111101 = 125 - 127 = -2 (Exponent is power of 2 of course).
Mantissa: 1000...... = 1.1 base 2 = 1.5
-1.5 x 2^(-2) = -0.375
Sign bit: 0 -> positive.
Exponent: 10000010 = 130 - 127 = 3.
Mantissa: 00010... = 1.0001 base 2 = 1.0625
1.0625 x 2^3 = 8.5
Line up exponents: I'll shift the -0.375 exponent by 5 to have the same exponent as the 8.5: -0.375 Mantissa only after shift: 0.000011
Next, I give both mantissas the same number of binary digits and precede them both with an extra zero. 01000100 and 00000011. The fact that they have 8 is coincidental. These are not bytes! Use as many digits as necessary from the mantissas.
Since the second one is supposed to be negative, take the 2's compliment: 11111101
Add:
01000100
11111101
101000001
Throw away extra leading bit when one is positive and one is negative: 01000001
Since the left most bit is 0 we have a positive number which should make sense. Strip it to leave the seven significant binary digits we started with after shifting for exponents: 1000001
Since the left bit is a one, we are good. We have 1.000001. Had you 'added' a bigger negative number, this left bit would become a zero and we would have to shift and change the exponent. But we do not. So......, It is positive (0), has exponent of 3 (10000010) we have:
0 1000 0010 000001000000........
Which happens to be 8.125
Upvotes: 2