Code:
BigInt &BigInt::operator/=(int rhs)
{
unsigned int i = this->len;
long long r = 0;
// Check for div by 0
if(rhs == 0)
return *this;
// Check signs
if(this->sign != (rhs < 0))
this->sign = !this->sign;
// Make rhs positive
if(rhs < 0)
rhs = -1 * rhs;
do{
i--;
// Get remainder and store in high word
r = (r % rhs) << sizeof(unsigned int) * 8;
// Get new low word
r |= this->data[i];
// Do and store division
this->data[i] = r / rhs;
}while(i != 0);
// For later use, (r >>= sizeof(unsigned int) * 8) equals the remainder
// Check for empty last block
if(this->data[this->len - 1] == 0)
this->resize(this->len - 1);
return *this;
}
const int BigInt::operator%(int rhs)
{
unsigned int i = this->len;
long long r = 0;
// Check for div by 0
if(rhs == 0)
return 0;
// Check signs
if(this->sign != (rhs < 0))
this->sign = !this->sign;
// Make rhs positive
if(rhs < 0)
rhs = -1 * rhs;
do{
i--;
// Get remainder and store in high word
r = (r % rhs) << sizeof(unsigned int) * 8;
// Clear low word
r &= ~((long long)(~((unsigned int)0)));
// Get new low word
r |= this->data[i];
// Do and store division
this->data[i] = r / rhs;
}while(i != 0);
return (int)(r >>= sizeof(unsigned int) * 8);
}
It's giving really weird output, and I'm too frustrated to find my likely stupid mistake.
The below prints 0:
Code:
#include <iostream>
#include "BigInt.hpp"
using namespace std;
int main()
{
BigNum::BigInt t;
t = 101;
t /= 3;
cout << t % 10 << endl;
return 0;
}