Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing addition using multiplication [closed]

I've been familiar with the famous question of implementing multiplication using addition, or exponentiation using multiplication, using algorithms of looping or bit-shifting and adding shifted bit groups combos.

Now, I wondered if there is any way to implement addition using only higher level operations, such as multiplication specifically, or exponentiation, logarithm etc. (subtraction excluded)

Can this be achieved with some algorithm combining these operations (and possibly bitwise operators as assistants) or is addition a fundamental operation that serves as an axiom, so it can't be reproduced in other ways except for its definition?

Thanks.

like image 761
Uriel Avatar asked Sep 14 '16 08:09

Uriel


2 Answers

Yes of course:

a + b = ln(exp(a) * exp(b))

Edit: Lifting my eyes above the practicality above to the more speculative, I would say that you should expect to be able to perform lower level operations through higher. As long as the higher level operation is built up by lower level operations, these should be able to perform at least what their building stones could. However probably not in an easy and straightforward way, see the comment below. A human can tell you that 1+1=2 but it would be much much cheaper and safer to ask a computer or an even simpler device.

like image 131
Mats Lind Avatar answered Sep 20 '22 11:09

Mats Lind


Well, if you want to be pedantic about it, two numbers in binary representation can be added up using only the bitwise operators OR, XOR and AND, without strictly "adding" anything. For the sum a + b = c, the n-th bit can be calculated like this:

cn = an XOR bn XOR carry
carry = (an AND bn) OR ((an XOR bn) AND carry)

And you should of course avoid using n++ when iterating over the bits, and throw in a couple of multiplications for good measure:

int add(int a, int b) {
    int c = 0;
    char carry = 0;
    for (int mask = 1; mask; mask *= 2) {
        char an = a & mask ? 1 : 0;
        char bn = b & mask ? 1 : 0;
        c |= (an ^ bn ^ carry) * mask;
        carry = an & bn | (an ^ bn) & carry;
    }
    return c;
}
like image 31