Recreate +operator

This is a discussion on Recreate +operator within the C++ Programming forums, part of the General Programming Boards category; So...I know how to do your basic + operator, but I was wondering if anyone knew of a way to ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    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. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    I don't know if I'm making sense...
    You're not. What does a loop have to do with the addition operator?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    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. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    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....
    Last edited by arcaine01; 08-01-2009 at 01:48 AM. Reason: Typos

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    This adds at the very least, these two values....
    That's because you changed the algorithm from your last post.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    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. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    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.
    Last edited by arcaine01; 08-01-2009 at 03:19 AM. Reason: Apologies

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by arcaine01 View Post
    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. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    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. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Sebastiani View Post

    >> 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 subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21