sundeepblue
4/17/2014 - 7:02 PM

Divide a number by 3 without using *, /, +, -, % operators

Divide a number by 3 without using *, /, +, -, % operators

// reference: http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators

int add(int a, int b) {
    int carry = (a&b) << 1;
    int sum = a ^ b;
    return carry == 0 ? sum : add(carry, sum);
}

int divide_by_3(int a) {
    ...
}