Hi
The following code shows good enough how to multiply two long data
long x = 10, y = 3;
long p = 0;
while (y > 0)
{
if ((y & 01) != 0) p += x;
x <<= 1;
y >>= 1;
}
std::cout << "\nResult : " << p;
But I want to make huge integers containing more than 32bit
I use an array of unsigned integers, but somehow this doesnt work
Note that DIGITS are defined as 20
class Unfinitebits
{
unsigned int m_DATA[DIGITS];
// ... operator overloading and stuff
};
Unfinitebits& Unfinitebits::operator*=(unsigned int value)
{
#ifndef NO_BITWISE
Unfinitebits p = 0;
while (value > 0)
{
if ((value & 01) != 0) p += *this;
*this <<= 1; // same as x above
value >>= 1; // same as y above
}
return p;
#else
// another method not yet implemented
#endif
}
Unfinitebits& Unfinitebits::operator+=(const Unfinitebits& rhs)
{
unsigned int* iter = &m_DATA[DIGITS-1];
const unsigned int* fiter = &rhs.m_DATA[DIGITS-1];
unsigned int keep;
for (int i = DIGITS-1;i > -1;i--)
{
keep = *fiter;
unsigned int help = *iter * keep;
if (help < *iter)
{
*iter = ~0; // Byte set of this integer is full
keep = help;
}
else
{
*iter *= *fiter;
break;
}
iter--;
fiter--;
}
return *this;
}
Unfinitebits& Unfinitebits::operator<<=(unsigned int value)
{
if (value == 0)
return *this;
unsigned int* iter = m_DATA;
for (int i = 0;i < DIGITS;i++)
*iter++ <<= value; // move all bits
return *this;
}
Unfinitebits& Unfinitebits::operator>>=(unsigned int value)
{
if (value == 0)
return *this;
unsigned int* iter = m_DATA;
for (int i = 0;i < DIGITS;i++)
*iter++ >>= value; // move all bits
return *this;
}
bool Unfinitebits::operator>(unsigned int value)
{
unsigned int* iter = m_DATA;
for (int i = 0;i < DIGITS-1;i++)
{
if (i < DIGITS-1 && *iter) // if a bit above 32 is set than it has to be greater
return true;
iter++;
}
return *iter > value; // otherwise compare the last integer with value
}
bool Unfinitebits::operator&(unsigned int value)
{
unsigned int* iter = m_DATA;
for (int i = 0;i < DIGITS-2;i++)
{
if (!*iter++ & 0) return false; // make sure no byte is set , since value can only concern the last integer
}
return m_DATA[DIGITS-1] & value; // compare the last integer
}
Thats what Unfinitebits does, but I don't know why it doesn't work.
I don't want you poeple to do my homework (I do it in my free time though) but to give me hints how I can work with it properly.
I attached the whole project, there is only Unfinitebits implemented.