8

I am 9 grade, My math teacher asked me to add numbers with out using + sign in C program.

I tried a - (-b) = a + b; but my math teacher want some other option.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 2
    Your solution is the one I thought about first. – Marcelo Sep 25 '13 at 16:58
  • 1
    He probably means bitwise then? Off the top of my head: AND to spot bits that'll carry, XOR to add; then shift the AND result left and repeat adding that to the XOR value until there's no AND left? – Rup Sep 25 '13 at 17:00
  • 1
    Perhaps try something more... iterative. ( think a `while` ) –  Sep 25 '13 at 17:00
  • How about [Addition of two integers using bitwise operators](http://stackoverflow.com/q/4068033/1168156) ? – LihO Sep 25 '13 at 17:00
  • Implement a full-adder. – Obicere Sep 25 '13 at 17:01
  • You genius you thought this in the 9th grade!! – Sadique Sep 25 '13 at 17:03
  • So how is Java related? Why is it tagged? – harold Sep 25 '13 at 17:03
  • Wow, a nice question. I'm gonna learn much with this question – Sri Harsha Chilakapati Sep 25 '13 at 17:07
  • 2
    You should really ask your prof to be more specific on the constraints. The logarithm-based approach below is elegant, but seems a bit too "easy". I'm suspecting your prof, given that this is a programming course, wants you to use a boolean-logic based approach, rather than an algebraic approach. +1 to all the solutions below! – Cloud Sep 25 '13 at 17:26
  • 4
    Although the question is rather interesting, this is asking to do his homework. – Rui Marques Sep 25 '13 at 18:37

5 Answers5

23

Use this function in your c program

int Add(int a, int b)
{
    while (b)
    {
        // carry now contains common set bits of "a" and "b"
        int carry = a & b;

        // Sum of bits of "a" and "b" where at least one of the bits is not set
        a = a ^ b;

        // Carry is shifted by one so that adding it to "a" gives the required sum
        b = carry << 1;
    }
    return a;
}
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Vikas Sardana
  • 1,593
  • 2
  • 18
  • 37
12

Use bitwise ^ and & operators and recursion

int add(int x, int y){
    return y == 0 ? x : add( x ^ y, (x & y) << 1);
}

P.S.: It is the recursive version of an algorithm proposed by vikas.

zavg
  • 10,351
  • 4
  • 44
  • 67
8

In Java using recursion-

public static int add(int a, int b){

    if(b == 0) return a;
    int sum = a ^ b;
    int carry = (a & b) << 1;

    return add(sum, carry);

}

In C-

int add(int a, int b){

    if(b == 0) return a;
    int sum = a ^ b;
    int carry = (a & b) << 1;

    return add(sum, carry);
}
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
7

Using Anti Log() you can do that

Temp= Anti Log(a)* Anti Log(b);

a+b value is equals to log(Temp);

Works for integers not for double.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
cvsr.sarma
  • 879
  • 1
  • 13
  • 29
2
#include<stdio.h>

int add(int a, int b) {
    return (int)&((char *)a)[b];
}

int main() {
    printf("%d", add(5, 17));
    getchar();
}

Not portable but doesn't use the "+" character. This casts a to a char pointer, adds b to it with [] and then casts it back to an int.