int add(int a, int b) {
int m = a ^ b,
n = a & b;
if( n != 0 )
return add(m, n << 1);
elsereturn m;
}
int subtract(int a, int b) {
int m = a ^ b,
n = ~a & b;
if( n != 0 )
return subtract(m, n << 1);
elsereturn m;
}
//----------------------------------------------------------------------------------------------------
#include <iostream>
usingnamespace std;
int main() {
int a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
int c = add(a, b), d = subtract(a, b);
cout << a << " + " << b << " = " << c << endl
<< a << " - " << b << " = " << d << endl;
return 0;
}
So the add & subtract functions are recursive and they keep looping until n = 0. Thats pretty cool. I was wondering if you have any examples of what the bitwise OR (|) is used for. I know how it works but I can't find ways to use it in a program.