From c155f79362bf69072aa0d17fed482e48b44487be Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:52:02 +0100 Subject: [PATCH] multiplication using binary enumeration --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index e8e4602..db11bdd 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -120,6 +120,18 @@ int multiply_by_odd(int num, int factor) { return result; } +int multiply_using_binary_enumeration(int num1, int num2) { + int result = 0; + while (num1 && num2) { + if (num2 & 1) { + result += num1; + } + num1 <<= 1; + num2 >>= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index ff24d5e..65e2e34 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -19,6 +19,7 @@ int multiply_by_prime(int num, int prime); int multiply_using_increment(int num1, int num2); int multiply_by_prime_and_its_square(int num, int prime); int multiply_by_odd(int num, int factor); +int multiply_using_binary_enumeration(int num1, int num2); #endif // MULTIPLICATION_H