SIr,

I wanted to know the C Statement for adding two numbers without using + sign..

Anyone can help me to solve this.....

ThanQ

Dani AI

Generated

Short answer: use bitwise operations to compute the sum and the carry separately, then repeat until there is no carry left. Several replies here already hinted at tricks: showed a subtraction/negation trick, pointed toward XOR/AND, and described the manual "columns with carry" idea — the code below puts the XOR/AND approach into a safe, practical C function.

unsigned int add(unsigned int a, unsigned int b) {
    while (b) {
        unsigned int carry = a & b;
        a = a ^ b;
        b = carry << 1;
    }
    return a;
}

Why this works: a ^ b produces the bitwise sum without carries (0+0->0, 1+0->1, 1+1->0). a & b isolates positions that generate carries; shifting that left applies those carries to the next bit. Repeat until there are no more carries and the XOR result is the final sum.

Practical notes and troubleshooting:

  • Use unsigned types to avoid undefined behaviour when left-shifting sign bits. If you need to work with signed ints, cast to an unsigned type of the same width, perform the algorithm, then cast back and handle overflow detection separately.
  • The loop iterates at most the number of bits in the type, so it is predictable but usually slower than the CPU add instruction — prefer + in production code for performance unless the constraint forbids it.
  • To extend to 64-bit values, switch to unsigned long long (or uint64_t) and test with edge cases like carry-out from the top bit.

This gives a correct, portable way to add two integers without ever using the + operator and directly implements the column/ carry idea that appears in the thread.

Recommended Answers

All 5 Replies

Try

int a = b - -c;

Stop the course which asks silly questions like this.

commented: Cute Trick [Grunt] +1
commented: LOL - the old favourites are the best - Salem +1

how about std::accumulate?
There won't be + operator insight ;)

> how about std::accumulate?
Don't remember this C statement :)

How would you do it on paper? Imagine you're back at infants school where they teach you how to add up using columns of hundreds/tens/units with a carrier at the bottom.

In an equally pointless vein

// a = a + b
while ( b-- ) a++;

OK, so there are some side effects, and a bug or two ;)

> I wanted to know the C Statement for adding two numbers without using + sign..
Study what exclusive-or and bitwise-and give you for a single pair of bits.
Hint: exclusive-or adds two bits without carry.
That's all I'm saying without giving the whole game away, and that's probably too much anyway.

commented: Nice-[Grunt] +1
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.