diff --git a/src/multiplication.c b/src/multiplication.c index b583823..143fa25 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -48,5 +48,17 @@ int multiply_by_sum(int num1, int num2) { return num1 * (num2 + 1); } +int multiply_using_two_complements(int num1, int num2) { + int result = 0; + while (num1 != 0) { + if (num1 & 1) { + result += num2; + } + num1 >>= 1; + num2 <<= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 77be6d8..dd99a1f 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -10,6 +10,7 @@ float multiply_by_percentage(float num, float percentage); int multiply_by_random(int num); int multiply_string(const char* str, int factor); int multiply_by_sum(int num1, int num2); +int multiply_using_two_complements(int num1, int num2); #endif // MULTIPLICATION_H