SIr,
I wanted to know the C Statement for adding two numbers without using + sign..
Anyone can help me to solve this.....
ThanQ
SIr,
I wanted to know the C Statement for adding two numbers without using + sign..
Anyone can help me to solve this.....
ThanQ
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:
+ in production code for performance unless the constraint forbids it.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.
Jump to Post— WolfPack 491Try
int a = b - -c;Stop the course which asks silly questions like this.
Jump to Post— andor 25> how about std::accumulate?
Don't remember this C statement :)
Try
int a = b - -c; Stop the course which asks silly questions like this.
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.