- //n = (2^n-) * a + b; n / (2^n-1) = a + (a + b) / (2^n-1);
- // 替换加法运算符
- 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); //获取a
- num = add(num >> 2, num & 3); 获取a+b;
- }
- if (num == 3)
- sum = add(sum, 1);
- return sum;
- }