Guest User

Untitled

a guest
Apr 20th, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. int recursiveAdd(int a, int b) {
  2.         int xor = a ^ b;
  3.         int and = (a & b) << 1;
  4.         if (and != 0)
  5.                 return recursiveAdd(xor, and);
  6.         return xor;
  7. }
  8.  
  9. int multiply(int a, int b) {
  10.     int total = 0;
  11.     int i = 0;
  12. loop:
  13.     if (a & (1 << i))
  14.         total = recursiveAdd(total, b << i);
  15.     i = recursiveAdd(i, 1);
  16.     if (i < sizeof(int) << 3)
  17.         goto loop;
  18.         //To really stop compiler abuse
  19.     return total;
  20. }
  21.  
  22. int recursiveSubtract(int a, int b) {
  23.     int xor = a ^ b;
  24.     int other = ((a ^ b) & b) << 1;
  25.     if (other != 0)
  26.         return recursiveSubtract(xor, other);
  27.     return xor;
  28. }
  29.  
  30. int divide(int a, int b) {
  31.     if (a < 0)
  32.         return -1;
  33.     return recursiveAdd(divide(recursiveSubtract(a, b), b), 1);
  34.     //I sort of gave up here.
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment