Program hangs at bitwise multiplication function

Hello all! So I'm doing a homework question asking me to code some Egyptian division, but that's not what I need help with. The problem is that I am only allowed to use bitwise operations for the first chunk of the program (until we get to fractions in the remainders).

I'm very new to functions and modularizing, and I just can't seem to get my intMultiply function to work. If you scroll down to my "getBinaryAnswer" function, you'll see one line commented out, and I've temporarily replaced it with a working but "illegal" line. If you have a simpler way to approach that line, I would also love to hear it.

Anyway, final thing on intMultiply. I tested it on its own inside main, and the program would just hang right before it. Is it because I did something wrong in using intAddition within intMultiply? Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int intAddition(int num1, int num2);
int intMultiply(int num1, int num2);
int getNumbers(int &num1, int &num2);
int getBinaryAnswer(int dividend, int divisor, int &binary, int &ans);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int dividend = 0, divisor = 0, counter = 1, binaryAns = 0;
    getNumbers(dividend, divisor);
    getBinaryAnswer(dividend, divisor, counter, binaryAns);
    cout<<endl;
    return a.exec();
}


int intAddition(int num1, int num2)
{
    int carry = 0, sum = 0;
    do
    {
        carry = num1 & num2;
        sum = num1 ^ num2;
        carry <<= 1;
    }while(carry);
    return(sum);
}


int intMultiply(int num1, int num2)
{
    int product = 0, i = 0;
    while(i < num2)
    {
        product = intAddition(num1, product);
        i = intAddition(i, 1);
    }
    return product;
}


int getNumbers(int &dividend, int &divisor)
{
    cout<<"Please enter the dividend."<<endl;
    cin>>dividend;
    cout<<"Please enter the divisor."<<endl;
    cin>>divisor;
    return 0;
}


int getBinaryAnswer(int dividend, int divisor, int &binary, int &ans)
{
    int divisorCounter = divisor;
    while (divisorCounter < dividend)
    {
        binary <<= 1;
        divisorCounter <<= 1;
    }
    while (divisorCounter > divisor)
    {
//     if (intAddition((intMultiply(ans, divisor)),divisorCounter) <= dividend)
       if (intAddition((ans*divisor),divisorCounter) <= dividend)
             ans |= binary;
       binary >>= 1;
       divisorCounter >>= 1;
    }
    cout<<ans;
    return ans;
}
1
2
3
4
5
6
7
8
9
10
11
int intAddition(int num1, int num2)
{
    int carry = 0, sum = 0;
    do
    {
        carry = num1 & num2;
        sum = num1 ^ num2;
        carry <<= 1;
    }while(carry);
    return(sum);
}


You'll notice that in intAddition carry and sum are assigned the same value in every iteration of the loop (which means the loop will never terminate.)
Wow, don't know how I ended up with that! Thanks for looking through and catching that. I didn't even notice because I was (stupidly) using numbers where no bits needed to be carried.

1
2
3
4
5
6
7
8
9
10
11
12
int intAddition(int num1, int num2)
{
    int carry = 0, sum = 0;
    do
    {
        carry = num1 & num2;
        sum = num1 ^ num2;
        num1 = carry << 1;
        num2 = sum;
    }while(carry);
    return(sum);
}


Much better.
Topic archived. No new replies allowed.