can i add two numbers without using arthemetic operators. can any one do it.
vvidyadhara 0 Newbie Poster
Salem 5,199 Posting Sage
> can i add two numbers without using arthemetic operators
I've no idea what you can or cannot do, but since you're asking, my guess is you can't.
> can any one do it.
I can.
http://www.catb.org/~esr/faqs/smart-questions.html#prune
iamthwee
Hint
#include <stdio.h>
#include <conio.h>
unsigned int add(unsigned int a, unsigned int b)
{
unsigned int c= 0;
unsigned int r= 0;
unsigned int t= ~0;
for (t= ~0; t; t>>= 1)
{r<<= 1;
r|= (a^b^c)&1;
c= ((a|b)&c|a&b)&1;
a>>= 1;
b>>= 1;}
for (t=~0, c=~t; t; t>>= 1)
{c<<= 1;
c|= r&1;
r>>= 1;}
return c;}
void main ( void ) //so much greater than int main
{
int t;
t = add (7,3);
printf("%d",t);
getch(); //pause program
system("pause"); //just incase getch don't work
}
Aia 1,977 Nearly a Posting Maven
In order for you to understand the answer to you question you need to know about XOR, AND and SHIFT bitwise operators.
Let's add 3 + 5:
integer1 = 3 = 0011b
integer2 = 5 = 0101b
first operation second operation third operation
0011 0011 shift by one
0101 0101
______ ______
XOR 0110 AND 0001 ------> <<1 0010
Again...
first operation second operation third operation
previous XOR 0110 0110 shift by one
previous <<1 0010 0010
______ ______
XOR 0100 AND 0010 ------> <<1 0100
Again...
first operation second operation third operation
previous XOR 0100 0100 shift by one
previous <<1 0100 0100
______ ______
XOR 0000 AND 0100 ------> <<1 1000
Again...
first operation second operation
previous XOR 0000 0000
previous <<1 1000 1000
______ ______
XOR 1000 AND 0000
When AND iguals 0 the result of XOR is iqual to the sum of the two integers
An example of that would be the following function:
#include <stdio.h>
unsigned long add(unsigned long integer1, unsigned long integer2)
{
unsigned long xor, and, temp;
and = integer1 & integer2; /* Obtain the carry bits */
xor = integer1 ^ integer2; /* resulting bits */
while(and != 0 ) /* stop when carry bits are gone */
{
and <<= 1; /* shifting the carry bits one space */
temp = xor ^ and; /* hold the new xor result bits*/
and &= xor; /* clear the previous carry bits and assign the new carry bits */
xor = temp; /* resulting bits */
}
return xor; /* final result */
}
int main( void )
{
int n1 = 5, n2 = 3;
printf( "%d + %d = %d\n", n1, n2, add( n1, n2 ) );
getchar();
return 0;
}
amitp49 0 Newbie Poster
//No use '+' operator : can be implemented as full adder circuit diagram
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
int lastbit(int num)
{
return (num & 0x01);
}
int sum_wo_op_use_circuit(int n1,int n2) //do not use '+' oprator for sum
{
int carry=0,p=0,g=0,i=0,s=0,len=0,temp=0,res=0;
int arr[MAX];
/*Assume : numbers are positive, else need to mul by -1*/
while(n1 || n2 || carry)
{
printf("Lastbit of [%d]: %d\n",n1,lastbit(n1));
printf("Lastbit of [%d]: %d\n",n2,lastbit(n2));
p = lastbit(n1) ^ lastbit(n2);
g = lastbit(n1) & lastbit(n2);
arr[i++] = p ^ carry;
temp = p & carry;
carry = temp | g;
//shift one bit from both n1 & 2
n1=n1>>1;
n2=n2>>1;
}
len=i-1;
//print the arr in reverse direction
for(i=len;i>=0;i--)
{
printf("%d\t",arr[i]);
}
//make the number
int mul = 1;
for(i=0;i<=len;i++)
{
res += arr[i] * (mul<<i);
}
return res;
}
int main(void)
{
int i=0;
int num1=5,num2=7;
i=sum_wo_op_use_circuit(num1,num2);
printf("Final Sum ... [%d] + [%d] = %d\n",num1,num2,i);
return 0;
}
Edited by amitp49 because: Heading change
karthi.k.s -7 Newbie Poster
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
for(;b>=0;b--)/* ONLY RELATIONAL AND INCREMENT DECREMENT OPERATORS ARE USED HERE*/
{
a++;
}
printf("The sum is : %d",a);
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.