1

I understood how this solution works.

int add_no_arithm(int a, int b) {
  if (b == 0) return a;
  int sum = a ^ b; // add without carrying
  int carry = (a & b) << 1; // carry, but don’t add
  return add_no_arithm(sum, carry); // recurse
}

But the author comments over this problem as:

"Our first instinct in problems like these should be that we’re going to have to work with bits. Why? Because when you take away the + sign, what other choice do we have? Plus, that’s how computers do it."

What is the author trying to imply?

0

2 Answers 2

3

What he means is quite simple - if you don't have the + operation, you'll need to replicate the behaviour on the bit level of an integer. The code you posted is about the same of what the + operation does natively in the ALU (algorithmic logical unit, the place where calculations take place in a CPU).

Sign up to request clarification or add additional context in comments.

Comments

1

Yeah, this is what is used - http://en.wikipedia.org/wiki/Adder_%28electronics%29

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.