Here is the program to add two numbers without using the '+' operator.
#include <iostream>
using namespace std;
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;
}
--- In [email protected], Mani Kandan <[EMAIL PROTECTED]> wrote:
>
> will it work for negative values?
>
>
>
>
> Saurabh Jain <[EMAIL PROTECTED]> wrote:
> --- In [email protected], Mohan S N <mohansnayaka@>
wrote:
> >
> > --- Ananth <ananth.tm@> wrote:
> >
> > > Thomas Hruska <thruska@> wrote:
> > > > parrot_rabbit4u wrote:
> > > > > --- In [email protected], senthil kumar
> > > <blackdiamonds2007@>
> > > > > wrote:
> > > > >> #Iinclude<iostream.h>
> > > > >> #include<conio.h>
> > > > >> main()
> > > > >> {
> > > > >> int a,b,c;
> > > > >> clrscr();
> > > > >> cout<<"enter the number:";
> > > > >> cin>>a>>b;
> > > > >> c=a-(-b);
> > > > >> cout<<"The add value:"<<c;
> > > > >> getch();
> > > > >> return 0;
> > > > >> }
> > > > >>
> > > > >> ur program will definetly work with out using '+' operator.
> > > > > thanks for ur this idea.
> > > >
> > > > This is fundamental math. Of the lightweight elementary school
> > > 'Algebra
> > > > I' variety. I still don't know why the OP thought they needed
to
> > > do
> > > > this...
> > >
> > > To answer a trick question?
> > > If learning C++, to impress friends and classmates?
> > > Should be one of the two.
> >
> > Le me throw in my 2p too...
> >
> > Putting a+b as a - ( -b ) is not impressive.
>
> Hmmm... I think it was +b sign being replaced by (-(-(b)). Why would
> that change anything? I did not quite get it. I assume replacing +
> with -- above does not modify the value of b in any way. Does it?
>
> ~Saurabh
>
> > See this:
> >
> > for(i=0;i<b;i++){
> > a++;
> > }
> >
> > OR
> >
> > while(b){
> > a++;
> > b--;
> > }
>
>
>
>
>
>
> ---------------------------------
> Did you know? You can CHAT without downloading messenger. Click
here
>
> [Non-text portions of this message have been removed]
>