Skip to content

Commit efe731d

Browse files
Implement Modulu (#10)
Co-authored-by: Jochen <97750753+Jochengehtab@users.noreply.github.com>
1 parent 0bb3745 commit efe731d

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

src/main.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,5 @@ int main(int argc, char* argv[]) {
5454
cli::help();
5555
}
5656

57-
//std::vector<uint8_t> num1 = operations::convertToVector(55555555);
58-
//std::vector<uint8_t> num2 = operations::convertToVector(45678);
59-
60-
// 00010000 00000000 00000000 00000000
61-
std::vector<uint8_t> num1 = {0b00000000, 0b00000000, 0b00000000, 0b00010000};
62-
// 00000000 00000100 00000000 00000000
63-
std::vector<uint8_t> num2 = {0b00000000, 0b00000000, 0b00000100};
64-
65-
std::vector<uint8_t> result = operations::mul(num1, num2);
66-
std::cout << operations::isBigger(num1, num2) << std::endl;
67-
68-
// This prints out the result as a hex number
69-
for (auto it = result.rbegin(); it != result.rend(); ++it) {
70-
printf("%02X", *it);
71-
}
72-
std::cout << std::endl;
73-
7457
return 0;
7558
}

src/vec/operations.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ namespace operations {
267267

268268
[[nodiscard]] std::vector<std::uint8_t> div(
269269
const std::vector<std::uint8_t> dividend,
270-
const std::vector<std::uint8_t> &divisor) noexcept {
270+
const std::vector<std::uint8_t> &divisor,
271+
std::vector<std::uint8_t> *remaining = nullptr) noexcept
272+
{
271273
std::vector<std::uint8_t> quotient;
272274
std::uint8_t quotientBuffer = 0;
273275
std::uint16_t quotientBitIndex = 0;
@@ -289,6 +291,17 @@ namespace operations {
289291
}
290292

291293
if (isEqual(dividendMask, divisor) || isBigger(dividendMask, divisor)) {
294+
295+
/* Stop the loop if the dividend is smaller than the divisor, because fractional
296+
* digits are not supported */
297+
if (dividendIndex < 0) {
298+
quotientBuffer <<= 1;
299+
300+
// If remaining pointer is passed, set the remaining value
301+
if (remaining != nullptr) *remaining = dividendMask;
302+
break;
303+
}
304+
292305
// Shift the dividend and set the new bit as high
293306
quotientBuffer <<= 1;
294307
quotientBuffer++;
@@ -301,6 +314,9 @@ namespace operations {
301314
* digits are not supported */
302315
if (dividendIndex < 0) {
303316
quotientBuffer <<= 1;
317+
318+
// If remaining pointer is passed, set the remaining value
319+
if (remaining != nullptr) *remaining = dividendMask;
304320
break;
305321
}
306322

0 commit comments

Comments
 (0)