diff --git a/src/main.cpp b/src/main.cpp index f7106d8..15e2ebd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,22 +54,5 @@ int main(int argc, char* argv[]) { cli::help(); } - //std::vector num1 = operations::convertToVector(55555555); - //std::vector num2 = operations::convertToVector(45678); - - // 00010000 00000000 00000000 00000000 - std::vector num1 = {0b00000000, 0b00000000, 0b00000000, 0b00010000}; - // 00000000 00000100 00000000 00000000 - std::vector num2 = {0b00000000, 0b00000000, 0b00000100}; - - std::vector result = operations::mul(num1, num2); - std::cout << operations::isBigger(num1, num2) << std::endl; - - // This prints out the result as a hex number - for (auto it = result.rbegin(); it != result.rend(); ++it) { - printf("%02X", *it); - } - std::cout << std::endl; - return 0; } diff --git a/src/vec/operations.h b/src/vec/operations.h index fc68774..c76a995 100644 --- a/src/vec/operations.h +++ b/src/vec/operations.h @@ -267,7 +267,9 @@ namespace operations { [[nodiscard]] std::vector div( const std::vector dividend, - const std::vector &divisor) noexcept { + const std::vector &divisor, + std::vector *remaining = nullptr) noexcept +{ std::vector quotient; std::uint8_t quotientBuffer = 0; std::uint16_t quotientBitIndex = 0; @@ -289,6 +291,17 @@ namespace operations { } if (isEqual(dividendMask, divisor) || isBigger(dividendMask, divisor)) { + + /* Stop the loop if the dividend is smaller than the divisor, because fractional + * digits are not supported */ + if (dividendIndex < 0) { + quotientBuffer <<= 1; + + // If remaining pointer is passed, set the remaining value + if (remaining != nullptr) *remaining = dividendMask; + break; + } + // Shift the dividend and set the new bit as high quotientBuffer <<= 1; quotientBuffer++; @@ -301,6 +314,9 @@ namespace operations { * digits are not supported */ if (dividendIndex < 0) { quotientBuffer <<= 1; + + // If remaining pointer is passed, set the remaining value + if (remaining != nullptr) *remaining = dividendMask; break; }