By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

How to add two numbers without using the plus operator?

P: n/a
How to add two numbers without using the plus operator?

Jun 6 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
co******@gmail.com wrote:
How to add two numbers without using the plus operator?


a-(-b)

Jun 6 '06 #2

P: n/a
aahhh!!!!
not this way.
using bitwise operators ..

Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?


a-(-b)


Jun 6 '06 #3

P: n/a
co******@gmail.com wrote:
aahhh!!!!
not this way.
using bitwise operators ..

Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?

a-(-b)


Give us your instructor's email address, and we'll send the answer
directly to him.
Jun 6 '06 #4

P: n/a
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

Jun 6 '06 #5

P: n/a
What are you trying to prove by this??
If u know its answer then post here...or else go to hell.
red floyd wrote:
co******@gmail.com wrote:
aahhh!!!!
not this way.
using bitwise operators ..

Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?
a-(-b)


Give us your instructor's email address, and we'll send the answer
directly to him.


Jun 6 '06 #6

P: n/a
On 6 Jun 2006 16:16:46 -0700, "co******@gmail.com"
<co******@gmail.com> wrote:
red floyd wrote:
co******@gmail.com wrote:
> aahhh!!!!
> not this way.
> using bitwise operators ..
>
> Markus Schoder wrote:
>> co******@gmail.com wrote:
>>> How to add two numbers without using the plus operator?
>> a-(-b)
>


Give us your instructor's email address, and we'll send the answer
directly to him.


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
Jun 6 '06 #7

P: n/a
In article <11**********************@u72g2000cwu.googlegroups .com>,
"co******@gmail.com" <co******@gmail.com> wrote:
How to add two numbers without using the plus operator?


Why would you want to?
Jun 7 '06 #8

P: n/a
In article <1149631877.720828.165900
@u72g2000cwu.googlegroups.com>, co******@gmail.com
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 7 '06 #9

P: n/a
In article <1149631877.720828.165900
@u72g2000cwu.googlegroups.com>, co******@gmail.com
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 7 '06 #10

P: n/a
Daniel T. wrote:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"co******@gmail.com" <co******@gmail.com> wrote:
How to add two numbers without using the plus operator?


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.

Jun 7 '06 #11

P: n/a
co******@gmail.com wrote:
What are you trying to prove by this??
If u know its answer then post here...or else go to hell.
red floyd wrote:

Give us your instructor's email address, and we'll send the answer
directly to him.

Hmmmm.
1. Homework cheater.

2. Top-poster.

3. Uses "u",

4. Rude.
I think that adds up to *plonk*.

Brian
Jun 7 '06 #12

P: n/a
Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?

a-(-b)


Or (-a-b)*(-1)
Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 7 '06 #13

P: n/a
Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?


a-(-b)


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

a += b;
// use a
a -= b;

Jun 8 '06 #14

P: n/a
On 6 Jun 2006 15:11:17 -0700, "co******@gmail.com"
<co******@gmail.com> wrote:
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
Jun 8 '06 #15

P: n/a
Old Wolf wrote:
Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?


a-(-b)


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.

Jun 8 '06 #16

P: n/a
Markus Schoder wrote:
Old Wolf wrote:
Markus Schoder wrote:
co******@gmail.com wrote:
How to add two numbers without using the plus operator?

a-(-b)


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.


Did you try it on all of them? What about future implementations?
I have heard of current ones that trap on integer overflow.

Jun 8 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.