@Puneet: Correct me if I am wrong, but when I try your code on binary
numbers a = 101 and b = 011, I get 001 instead of 1000. Try this:
int add(int a, int b)
{
int c;
while(b)
{
c = a & b;
a ^= b;
b = c << 1;
}
return a;
}
Dave
On Aug 20, 4:06 pm, Puneet Chawla <[email protected]> wrote:
> For subtraction we can use the same add function
>
> int add(int a, int b)
> {
> while (a) {
> a = (a & b) << 1;
> b = a^b;
> }
> return b;
> }
>
> int sub(int a, int b) // add a with b's 2's
> complement.
> {
> return (add(a, add(~b, 1)));
>
> }
>
> On Sat, Aug 20, 2011 at 11:34 PM, priya ramesh <
>
> [email protected]> wrote:
> > +1 sagar!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> With regards
> ............
> Puneet Chawla
> Computer Engineering Student
> NIT Kurukshetra
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.