I'm making a class I think I have it mostly working correctly, but the division has stumped me. I have the part that I'm working on in the main I got it off of here. Unfortunately, I can't get the division code to work with my class. It works fine with uin64 and I don't know what part of my class isn't working.
I have found many bugs in my code and fixed them. I also did a thorough test of the code that the division function relies on but it still refuses to work
class uint128_t {
public:
unsigned long long int data[2];
uint128_t() {
data[1] = 0;
data[0] = 0;
}
uint128_t operator~() {
uint128_t out;
out.data[0] = ~data[0];
out.data[1] = ~data[1];
return out;
}
uint128_t operator+(const uint128_t& b) {
uint128_t out;
out.data[1] = data[1] + b.data[1];
out.data[0] = data[0] + b.data[0];
if (b.data[0] > (UINT8_MAX - data[0]))out.data[1]++;
return out;
}
uint128_t operator+(const unsigned long long int& b) {
uint128_t out;
out.data[1] = data[1];
out.data[0] = data[0] + b;
if (b > (UINT64_MAX - data[0]))out.data[1]++;
return out;
}
uint128_t operator-(const uint128_t& b) {
uint128_t out;
out.data[1] = data[1] + ~b.data[1];
out.data[0] = data[0] + ~b.data[0];
if (b.data[0] > (UINT8_MAX - data[0]))out.data[1]++;
out.data[0]++;
out.data[1]++;
return out;
}
bool operator<(const uint128_t& b) {
if (data[1] == b.data[1]) {
return (data[0] < b.data[0]) ? true : false;
}
else return (data[1] < b.data[1]) ? true : false;
}
bool operator>=(const uint128_t& b) {
if (data[1] == b.data[1]) {
return (data[0] >= b.data[0]) ? true : false;
}
else return (data[1] > b.data[1]) ? true : false;
}
uint128_t &operator=(const int& b) {
data[1] = 0;
data[0] = b;
return *this;
}
uint128_t& operator=(const uint128_t& b) {
data[1] = b.data[1];
data[0] = b.data[0];
return *this;
}
};
int main()
{
uint128_t divisor;
divisor = 2;
uint128_t quot, rem, t;
int bits_left = 128;
quot = 128;
rem = 0;
do {
// (rem:quot) << 1
t = quot;
quot = quot + quot;
rem = rem + rem + (quot < t);
if (rem >= divisor) {
rem = rem - divisor;
quot = quot + 1;
}
bits_left--;
} while (bits_left);
std::cout << std::bitset<64>(quot.data[1]) << std::bitset<64>(quot.data[0]) << std::endl;
}
So 128(quot)/2(divisor) = 64 but every time I run it I get some gibberish. But if I change the type from my class to uint64
it works fine