Making a Variable-adding function without operator +

This is a discussion on Making a Variable-adding function without operator + within the C++ Programming forums, part of the General Programming Boards category; I've just completed a function that adds two 16-bit numbers without using the + operator. If you are interested, check ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Lightbulb Making a Variable-adding function without operator +

    I've just completed a function that adds two 16-bit numbers without using the + operator.
    If you are interested, check out the attached file... or have a look below...

    (This function has no practical use, since it is way slower than +. I just made it for fun)


    Code:
    //--- Included files ---
    #include <iostream.h>
    #include <conio.h>
    
    
    //--- Global stuff ---
    bool MemBit=0;
    
    
    //--- Bit adder ---
    bool BitAdd(bool Bit1, bool Bit2)
    {
       //Add two bits
       MemBit=(Bit1 & Bit2);
       return (!(Bit1 & Bit2) && (Bit1 | Bit2));
    }
    
    
    //--- Set specific byte function ---
    void SetBit(unsigned short& TempNum, unsigned short Number)
    {
       unsigned short AddBit=1;
       AddBit=AddBit<<Number;
       TempNum=(TempNum | AddBit);
    }
    
    
    //--- Full adder ---
    unsigned short Add(unsigned short Num1, unsigned short Num2)
    {
       //Variables
       unsigned short CompleteNumber=0;
       bool TempBit;
       bool OldMemBit;
       bool PassOnBit;
    
       //Adds two 16-bit numbers using BitAdd function
       TempBit=BitAdd(Num1 & 1, Num2 & 1);
       if(TempBit) SetBit(CompleteNumber, 0);
       PassOnBit=MemBit;
    
       TempBit=BitAdd(Num1 & 2, Num2 & 2);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 1);
    
       TempBit=BitAdd(Num1 & 4, Num2 & 4);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 2);
    
       TempBit=BitAdd(Num1 & 8, Num2 & 8);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 3);
    
       TempBit=BitAdd(Num1 & 16, Num2 & 16);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 4);
    
       TempBit=BitAdd(Num1 & 32, Num2 & 32);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 5);
    
       TempBit=BitAdd(Num1 & 64, Num2 & 64);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 6);
    
       TempBit=BitAdd(Num1 & 128, Num2 & 128);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 7);
    
       TempBit=BitAdd(Num1 & 256, Num2 & 256);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 8);
    
       TempBit=BitAdd(Num1 & 512, Num2 & 512);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 9);
    
       TempBit=BitAdd(Num1 & 1024, Num2 & 1024);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 10);
    
       TempBit=BitAdd(Num1 & 2048, Num2 & 2048);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 11);
    
       TempBit=BitAdd(Num1 & 4096, Num2 & 4096);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 12);
    
       TempBit=BitAdd(Num1 & 8192, Num2 & 8192);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 13);
    
       TempBit=BitAdd(Num1 & 16384, Num2 & 16384);
       OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 14);
    
       TempBit=BitAdd(Num1 & 32768, Num2 & 32768);
       //OldMemBit=MemBit;
       TempBit=BitAdd(TempBit, PassOnBit);
       //PassOnBit=(OldMemBit | MemBit);
       if(TempBit) SetBit(CompleteNumber, 15);
    
       //Return answer
       return CompleteNumber;
    }
    
    
    //--- Main ---               
    int main()
    {
       //Add numbers
       cout << "1 plus 3 is:     " << Add(1, 3) << endl;
       cout << "6 plus 14 is:    " << Add(6, 14) << endl;
       cout << "256 plus 768 is: " << Add(256, 768) << endl;
       cout << "1100 plus 11 is: " << Add(1100, 11) << endl;
       cout << "65535 plus 1 is: " << Add(65535, 1);
       cout << "\t(Data out of range, restarts from 0)" << endl;
       cout << "65535 plus 2 is: " << Add(65535, 2) << endl;
       cout << "65535 plus 9 is: " << Add(65535, 9) << endl;
    
       //Exit
       getch();
       return 0;
    }
    Last edited by Magos; 09-04-2001 at 03:05 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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