简体   繁体   中英

Bugs when multiplying numbers with bitwise operators

I am trying to multiply two floating-point numbers using bitwise operators in IEEE-754 format. The 32-bit number is composed in the form sign - exponent - mantissa . After multiplying each number, the resultant answer is correct some of the time but not all of the time.

I think it has something to do with the resulting answer not being in normalized form (eg 1.1010101 * 2 5 ), but I don't know how to fix it.

#include <csdtdio>

struct Real
{    
   int sign;
   long exponent;
   unsigned long fraction;
};

Real Multiply(Real Val1, Real Val2){
   Real answer;
   answer.fraction = left.fraction + right.fraction;
   answer.exponent = left.exponent  + right.exponent;
   answer.sign = left.sign ^ right.sign;
   return  answer;
}

While multiplying the mantissa parts must be multiplied together, not add

(-1) sign1 × 2 exp1 × mantissa1 * (-1) sign2 × 2 exp2 × mantissa2
= (-1) sign1 + sign2 × 2 exp1 + exp2 × mantissa1 × mantissa2

And you don't need a separate variable for returning

Real Multiply(Real Val1, Real Val2){
   Val1.fraction *= Val2.fraction;
   Val1.exponent += Val2.exponent;
   Val1.sign ^= Val2.sign;
   return Val1;
}

After those basic things you'll still have to do normalization, for which you need to get the full result instead of just the low bits like the normal non-widening multiplication. Therefore you must cast your "fraction" (if you're using IEEE-754 then the correct term for it is significand ) to a wider type. Depending on which platform you're on, you may or may not have a type twice as big as an unsigned long . It's better to used fixed-width types like int32_t , uint64_t in this case. That's all the hints needed to do this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM