#include "stdio.h"
int add(int,int);
int subtract(int,int);
int positive_multiply(int,int);
int multiply(int,int);
int positive_divide(int,int);
int divide(int,int);
int main(void)
{
int result;
result=add(20,100);
printf("%d\n",result);
result=subtract(2,-1);
printf("%d\n",result);
result=multiply(-100,100);
printf("%d\n",result);
result=divide(-2147483647,6);
printf("%d\n",result);
return 0;
}
int add(int num1,int num2)
{
int sum=num1;
int carry=num2;
int temp;
while(carry)
{
temp=sum^carry;
carry=(sum&carry)<<1;
sum=temp;
}
return sum;
}
int subtract(int num1,int num2)
{
num2=add(~num2,1);
return add(num1,num2);
}
int positive_multiply(int num1,int num2)
{
int temp = 0;
while(num2)
{
if(num2&1)
temp = add(temp,num1);
num1= (num1<<1);
num2= (num2>>1);
}
return temp;
}
int multiply(int num1,int num2)
{
if(num1==0||num2==0)
return 0;
if(num1>0)
{
if(num2>0)
return positive_multiply(num1,num2);
else
return add(~positive_multiply(num1,add(~num2,1)),1);
}
else
{
if(num2>0)
return add(~positive_multiply(add(~num1,1),num2),1);
else
return positive_multiply(add(~num1,1),add(~num2,1));
}
}
int positive_divide(int dividend,int divisor)
{
int remainder=0;
int quotient=0;
int i;
for(i=sizeof(int)*8-2;i>=0;i--)
{
remainder=add(remainder<<1,(dividend>>i)&1);
quotient=quotient<<1;
if(remainder>=divisor)
{
remainder=add(remainder,add(~divisor,1));
quotient=add(quotient,1);
}
}
return quotient;
}
int divide(int dividend,int divisor)
{
if(divisor==0)
{
printf("divide() Error:divisor can't be zero!\n");
return -1;
}
if(dividend==0)
return 0;
if(dividend>0)
{
if(divisor>0)
return positive_divide(dividend,divisor);
else
return add(~positive_divide(dividend,add(~divisor,1)),1);
}
else
{
if(divisor>0)
return add(~positive_divide(add(~dividend,1),divisor),1);
else
return positive_divide(add(~dividend,1),add(~divisor,1));
}
}
int add(int,int);
int subtract(int,int);
int positive_multiply(int,int);
int multiply(int,int);
int positive_divide(int,int);
int divide(int,int);
int main(void)
{
int result;
result=add(20,100);
printf("%d\n",result);
result=subtract(2,-1);
printf("%d\n",result);
result=multiply(-100,100);
printf("%d\n",result);
result=divide(-2147483647,6);
printf("%d\n",result);
return 0;
}
int add(int num1,int num2)
{
int sum=num1;
int carry=num2;
int temp;
while(carry)
{
temp=sum^carry;
carry=(sum&carry)<<1;
sum=temp;
}
return sum;
}
int subtract(int num1,int num2)
{
num2=add(~num2,1);
return add(num1,num2);
}
int positive_multiply(int num1,int num2)
{
int temp = 0;
while(num2)
{
if(num2&1)
temp = add(temp,num1);
num1= (num1<<1);
num2= (num2>>1);
}
return temp;
}
int multiply(int num1,int num2)
{
if(num1==0||num2==0)
return 0;
if(num1>0)
{
if(num2>0)
return positive_multiply(num1,num2);
else
return add(~positive_multiply(num1,add(~num2,1)),1);
}
else
{
if(num2>0)
return add(~positive_multiply(add(~num1,1),num2),1);
else
return positive_multiply(add(~num1,1),add(~num2,1));
}
}
int positive_divide(int dividend,int divisor)
{
int remainder=0;
int quotient=0;
int i;
for(i=sizeof(int)*8-2;i>=0;i--)
{
remainder=add(remainder<<1,(dividend>>i)&1);
quotient=quotient<<1;
if(remainder>=divisor)
{
remainder=add(remainder,add(~divisor,1));
quotient=add(quotient,1);
}
}
return quotient;
}
int divide(int dividend,int divisor)
{
if(divisor==0)
{
printf("divide() Error:divisor can't be zero!\n");
return -1;
}
if(dividend==0)
return 0;
if(dividend>0)
{
if(divisor>0)
return positive_divide(dividend,divisor);
else
return add(~positive_divide(dividend,add(~divisor,1)),1);
}
else
{
if(divisor>0)
return add(~positive_divide(add(~dividend,1),divisor),1);
else
return positive_divide(add(~dividend,1),add(~divisor,1));
}
}