How to add two numbers without using the plus operator?

P

persenaama

I'd begin with something like this..

a b c v
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 2
1 0 0 1
1 0 1 2
1 1 0 2
1 1 1 3

Where the legends are: a,b are the bits from each respective addition
parameter being currently handled, c carry from previous bit and v the
sum of bits to be handled for current iteration. The sum, in short, can
be used as a class to handle different cases for current iteration, in
the below code I took the liberty of doing such.

// v = a + b
int a = ...;
int b = ...;

int v = 0;
int carry = 0;
for ( int i=0; i<N; ++i )
{
int sum = (a & 1) | ((b & 1) << 1) | (carry << 2);
a >> 1;
b >> 1;

// TODO: update v ;-o
// HINT: possibly most trivial to rewrite below switch statement
// and just handle each case separately, then look for similiarities
and
// see if a pattern emerge.. then take advantage of the fact and
rewrite it into
// code structured differently :) :)

switch ( sum ) {
case 1: case 2: case 4: case 7: carry = 1; break;
default: carry = 0; break;
}
}

I don't know if it is allowed to use "++", which is increment by your
standards and not the same thing as addition. It may well not be as it
would under some circumastances work against the spirit of the question
(as increment is a small cousin of addition :)

Depending on this the implementation on the "TODO" part varies heavily,
also you might want to rewrite the for loop to not use increment but
something more along the lines of:

int mask = ???; // PROFIT!
for ( int ref=1; !(ref & mask); ref <<= 1; ) { ...

But this approach, especially when using non-signed mask would cause
the number of bits being added to be limited in a way that might not be
desirable. Again something your question never communicated all too
clearly.

What's this for anyway? You implementing addition in something like
SystemC? I beg your pardon, but you *may* use addition in the
implementation if that is the case. :)

Oh wait, this is off-topic isn't it? ;-o
 
C

codergem

What are you trying to prove by this??
If u know its answer then post here...or else go to hell.
 
R

rossum

What are you trying to prove by this??
If u know its answer then post here...or else go to hell.
You are asking us for help. It pays to be polite if you are asking
for help. If you are not polite we may decide that we don't want to
help you.

All you need to do is to create a successor() function and use the
Peano axioms.

rossum
 
J

Jerry Coffin

@u72g2000cwu.googlegroups.com>, (e-mail address removed)
says...
How to add two numbers without using the plus operator?

Just about any digital design book should have the
circuitry for a half-adder and a full-adder.

Each bit of result is the XOR of its inputs, and each
carry bit is the OR of its inputs.
 
J

Jerry Coffin

@u72g2000cwu.googlegroups.com>, (e-mail address removed)
says...
How to add two numbers without using the plus operator?

Just about any digital design book should have the
circuitry for a half-adder and a full-adder.

Each bit of result is the XOR of its inputs, and each
carry bit is a one if two or more of the input bits are
one.
 
R

red floyd

Daniel said:
Why would you want to?

Because that's his homework assignment. He already was given an answer
a - (-b), and bitched at us that it wasn't what he wanted.
 
D

Default User

What are you trying to prove by this??
If u know its answer then post here...or else go to hell.
red floyd wrote:


Hmmmm.


1. Homework cheater.

2. Top-poster.

3. Uses "u",

4. Rude.


I think that adds up to *plonk*.



Brian
 
Z

Zara

How to add two numbers without using the plus operator?

This will not, compile, take the ideas:

while (carry!=0) {
sum=a xor carry;
carry=(a and carry) to the left one bit;
}

You must find what to put in sum and carry initiallly. This downgrades
the homework a couple of levels.

If you don't understand it, read the message from presenaama.

Regards, and do your homework!

Zara
 
M

Markus Schoder

Old said:
Doesn't work if they are ints and b is INT_MIN and INT_MIN < -INT_MAX.

Yes, I guess the standard labels this as undefined behaviour. In
reality it will work even for this corner case just fine with every
existing implementation.
 
O

Old Wolf

Markus said:
Yes, I guess the standard labels this as undefined behaviour. In
reality it will work even for this corner case just fine with every
existing implementation.

Did you try it on all of them? What about future implementations?
I have heard of current ones that trap on integer overflow.
 
Joined
Dec 17, 2008
Messages
1
Reaction score
0
include<stdio.h>

int add(int x, int y) {
int a, b;
do {
a = x & y;
b = x ^ y;
x = a << 1;
y = b;
} while (a);
return b;
}


int main( void ){
printf( "2 + 3 = %d", add(2,3));
return 0;
}

include<stdio.h>

int add(int x, int y) {
int a, b;
do {
a = x & y;
b = x ^ y;
x = a << 1;
y = b;
} while (a);
return b;
}


int main( void ){
printf( "2 + 3 = %d", add(2,3));
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top