Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int recursiveAdd(int a, int b) {
- int xor = a ^ b;
- int and = (a & b) << 1;
- if (and != 0)
- return recursiveAdd(xor, and);
- return xor;
- }
- int multiply(int a, int b) {
- int total = 0;
- int i = 0;
- loop:
- if (a & (1 << i))
- total = recursiveAdd(total, b << i);
- i = recursiveAdd(i, 1);
- if (i < sizeof(int) << 3)
- goto loop;
- //To really stop compiler abuse
- return total;
- }
- int recursiveSubtract(int a, int b) {
- int xor = a ^ b;
- int other = ((a ^ b) & b) << 1;
- if (other != 0)
- return recursiveSubtract(xor, other);
- return xor;
- }
- int divide(int a, int b) {
- if (a < 0)
- return -1;
- return recursiveAdd(divide(recursiveSubtract(a, b), b), 1);
- //I sort of gave up here.
- }
Advertisement
Add Comment
Please, Sign In to add comment