# Thread: Recreate +operator

1. ## Recreate +operator

So...I know how to do your basic + operator, but I was wondering if anyone knew of a way to do it without using a loop?
Like...can you do it using a formula?

If you know how many times you need to run the loop, and how many times you are going to manipulate the bits through the algorithm, could you write it as a formula rather than an algorithm? I thought the entire point of an algorithm was to work with unknown variable numbers, where formulas used constant data? I don't know if I'm making sense...

But yeah...what's the most efficient way to recreate the + operator that you know? (preferably using only bitwise operators)

2. I don't know if I'm making sense...
You're not. What does a loop have to do with the addition operator?

3. well....the + operator does this, in its most basic form.

Code:
```while(A&B)
{
A = A^B;
B = (A & B) << 1;
}
return B;```
I don't want the while loop though, because its not efficient enough.

Does anyone know a better method for addition? Or know of a way to do it through a formula, rather than an algorithm (which uses a loop)?

4. well....the + operator does this, in its most basic form.
No it doesn't. First of all, that algorithm doesn't even make sense. It sure as heck doesn't add two values together. Second of all, addition of integer values is done in hardware -- there is no way to optimize it further in code.

5. I understand that it is not going to make it more efficient, but how does it not add two values together?

take:
1010 (10)
+0010 (2)

int A = 10, B = 2;
while(A & B) //comes out to 0010 which is not 0x0, which means it is true (true is not false)
{
A = A ^ B; //comes out to 1000 (8)
B = (A & B) << 1; //comes out to 0010, and then 0100
}
//new while condition is (1000 & 0100) which equals 0x0, which is false, breaking the loop.
return A & B; // returns 1100; (12)

This adds at the very least, these two values....

6. This adds at the very least, these two values....
That's because you changed the algorithm from your last post.

7. Okay...well I changed the return value, but not the actual algorithm. Instead of critiquing my question, would you please just answer it instead?

8. Okay...well I changed the return value, but not the actual algorithm.
The return is part of the algorithm. The first one was wrong, and you changed it in the second post. Don't try and pretend like I am nit-picking when the two posts had code which generated completely different answers.

As for your answer -- there isn't one. You are asking for an optimization which can't possibly exist. Addition is performed using transistor logic gates. There is no loop because the entire addition can be performed in parallel by multiple gates. If you want to know how this is done, then read up on how an ALU works.

If your question instead is just how can I make my addition code more efficient without actually using the CPU's ADD instruction, well that's different.

9. I realize that the hardware does not use a loop and that piece of code, I was just trying to emulate carrying bits in a simple function, and was wondering how to accomplish that without using a loop - if that is at all possible. (Sorry if this was not clear from the beginning.)

Although you did gain my interest when you said:
If your question instead is just how can I make my addition code more efficient without actually using the CPU's ADD instruction, well that's different.

If you know of ways to optimize anything, I'm happy to listen

-Also, thank you for taking your time to explain this to me.

10. Originally Posted by arcaine01
I realize that the hardware does not use a loop and that piece of code, I was just trying to emulate carrying bits in a simple function, and was wondering how to accomplish that without using a loop - if that is at all possible. (Sorry if this was not clear from the beginning.)

Although you did gain my interest when you said:

If you know of ways to optimize anything, I'm happy to listen

-Also, thank you for taking your time to explain this to me.
Well, that's the catch, isn't it. ADD is probably one of the fastest operations, if not the fastest, in the whole instruction set, so ....

11. I'm not talking about improving the existing ADD. I'm just wondering how to improve my own ADD method so I can understand more, and learn some cool calculation tricks.

12. >> while(A & B)

And what if A == 2 and B == 1? At any rate, I don't think your algorithm is correct - it looks like a broken half-adder, to me. The way a half-adder works is:

sum = b1 ^ b2
carry = b1 & b2

A full adder would be:

sum = ( b1 ^ b2 ) ^ carry
carry = ( ( b1 ^ b2 ) & carry ) | ( b1 & b2 )

That's for each bit, naturally, and AFAIK, you can't do it on all of them at once since you need to know the value the previous carry bit, but I'm not 100% sure about that.

>> which means it is true (true is not false)

Flawless logic there.

13. Originally Posted by Sebastiani

>> which means it is true (true is not false)

Flawless logic there.
unless you explicitly state that true IS false (and false is not false)

Popular pages Recent additions