From c9ef902922cab37921ab5fd18bac0d6740e44bc3 Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 01:20:15 +0100 Subject: [PATCH] multiplication using logical operations --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 9cbc11f..06056de 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -80,5 +80,17 @@ int multiply_using_lookup_table(int num1, int num2) { return lookup_table[num1][num2]; } +int multiply_using_logical_operations(int num1, int num2) { + int result = 0; + while (num2) { + if (num2 & 1) { + result ^= num1; + } + num1 <<= 1; + num2 >>= 1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index 378e860..b242c9b 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -13,6 +13,7 @@ int multiply_by_sum(int num1, int num2); int multiply_using_two_complements(int num1, int num2); int multiply_by_even(int num, int factor); int multiply_using_lookup_table(int num1, int num2); +int multiply_using_logical_operations(int num1, int num2); #endif // MULTIPLICATION_H