V.1 底层使用加法累加实行
//以下部分实现了取因数的绝对值进而忽略因数符号的影响
int val1_1 = val1 >0 ? val1 :Add2(~val1,1);
int val2_t = val2 >0 ? val2 :Add2(~val2,1);
//有符号的加法通过无符号的加法实现
int sum = 0;
int count = 0;
while(count < val2_t)
{
sum = Add (sum,val1_t);
count = Add(count,1);
}
//judging if the sum is more than 0
if( (val1^val2) < 0)
{
sum = Add(~sum,1);
}
return sum;
v.2 使用二进制乘法的进位规律实现十进制乘法
原理:
// meaning to retain the solution number
int val1_t = val1>0 ? val1 : Add(~val1,1);
int val2_t = val2>0 ? val2 : Add(~val2,1);
int sum = 0;
while(val2_t > 0)
{
if((val2_t & 0x1) > 0)
{
sum = Add(sum,val1_t);
}
val1_t<<=1;//however whether val2_t is 0 or not ,the val1_t must be metivated!
val2_t>>=1;
}
if ((val1 ^ val2) < 0)
{
sum = Add( ~sum,1);
}
return sum;