知道很无聊一个题,拿出来解闷。bit shift operator都被人玩出好多花样了。 http://stackoverflow.com/questions/11694546/divide-a-number-by-
// replaces the + operator
int add(int x, int y) {
int a, b;
do {
a = x & y;
b = x ^ y;
x = a << 1;
y = b;
} while (a);
return b;
}
int divideby3 (int num) {
int sum = 0;
while (num > 3) {
sum = add(num >> 2, sum);
num = add(num >> 2, num & 3);
}
if (num == 3)
sum = add(sum, 1);
return sum;
}