标签:
/////任意正整数的加法,不使用加减乘除运算符实现正数的加法.
#include <iostream>
using namespace std;
int add(int x,int y)
{
int k = 0;
int result = 0;
int flog = 0;
while( x!=0 || y!=0 || flog!=0)
{
int i = x&0x1;
int j = y&0x1;
result = ((i^j^flog)<<k) | result;
k++;
int haha=0;
if((i==1 && j!=1) || (i!=1 && j==1))
{
if(flog==1)
{
flog=1;
haha=1;
}
else
flog=0;
}
if(i==1 && j==1)
flog = 1;
else
if(!haha)
{
flog = 0;
}
x>>=1;
y>>=1;
}
return result;
}
int main()
{
cout<<add(1000,3)<<endl;
return 0;
}标签:
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/45477395