Browse Source

multiplication using two complement

remotes/origin/multiplication
fdai7930 11 months ago
parent
commit
f112747e8a
  1. 12
      src/multiplication.c
  2. 1
      src/multiplication.h

12
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;
}

1
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

Loading…
Cancel
Save