tags:

views:

220

answers:

3

hi, i tried with the following code , but i can't understand why it's giving me wrong answer. i am computing the 2's complement and adding with another no.

#include <stdio.h>

int add(int a, int b) {
    while (a) {
        a = (a & b) << 1;
        b = a^b;
    }
    return b;
}

int sub(int a, int b) // add a with b's 2's complement.
{
    return (add(a, add(~b, 1)));
}

int main() {
    int a, b, res;
    a = 3, b = 1;
    res = sub(a, b);
    printf("%d\n", res);
    return 0;
}
+2  A: 

Here is better solution
http://stackoverflow.com/questions/1149929/

#include <stdlib.h> /* atoi() */
#include <stdio.h>  /* (f)printf */
#include <assert.h> /* assert() */

int add(int x, int y) {
    int carry = 0;
    int result = 0;
    int i;

    for(i = 0; i < 32; ++i) {
        int a = (x >> i) & 1;
        int b = (y >> i) & 1;
        result |= ((a ^ b) ^ carry) << i;
        carry = (a & b) | (b & carry) | (carry & a);
    }

    return result;
}

int negate(int x) {
    return add(~x, 1);
}

int subtract(int x, int y) {
    return add(x, negate(y));
}

int is_even(int n) {
    return !(n & 1);
}

int divide_by_two(int n) {
    return n >> 1;
}

int multiply_by_two(int n) {
    return n << 1;
}

int multiply(int x, int y) {
    int result = 0;

    if(x < 0 && y < 0) {
        return multiply(negate(x), negate(y));
    }

    if(x >= 0 && y < 0) {
        return multiply(y, x);
    }

    while(y > 0) {
        if(is_even(y)) {
                x = multiply_by_two(x);
                y = divide_by_two(y);
        } else {
                result = add(result, x);
                y = add(y, -1);
        }
    }

    return result;
}

int main(int argc, char **argv) {
    int from = -100, to = 100;
    int i, j;

    for(i = from; i <= to; ++i) {
        assert(0 - i == negate(i));
        assert(((i % 2) == 0) == is_even(i));
        assert(i * 2 == multiply_by_two(i));
        if(is_even(i)) {
                assert(i / 2 == divide_by_two(i));
        }
    }

    for(i = from; i <= to; ++i) {
        for(j = from; j <= to; ++j) {
                assert(i + j == add(i, j));
                assert(i - j == subtract(i, j));
                assert(i * j == multiply(i, j));
        }
    }

    return 0;
}
org.life.java
thanks but i was trying to use the add funtion mentioned in a post below it by Tom Leys , donno why it's not working
pranay
this is from http://stackoverflow.com/questions/1149929/how-to-add-two-numbers-without-using-or-or-another-arithmetic-operator/1150180#1150180 please vote for it there
Dan D
A: 

i used a different add() function as suggested by NullUserException, it works now:

int add(int a,int b)
{
int x;
x=a^b;
while(a&b)
{
    b=((a&b)<<1);
    a=x;
    x=a^b;
    //b=(a^b);
}
return x;
}
pranay
A: 

Considering how negative numbers are represented, the following will compute a - b:

int a, b, c;
// assign to a and b
c = a + (~b + 1); // () not needed, just to show the point

as the OP already noted:) This moves the attention to your add implementation, that is of course wrong. The following is an odd way to do it (just since other better ways are already given)

int add1(int a, int b, int *c)
{
  int r = *c & 1;
  a &= 1; b &= 1;
  *c = a&b | a&r | b&r;
  return a^b^r;
}
int inv(int a)
{
  int i, r = 0;
  for(i = 0; i < sizeof(int)*8; i++)
  {
    r = r<<1 | (a&1);
    a >>= 1;
  }
  return r<<1;
}
int add(int a, int b)
{
  int r = 0, i;
  int c = 0;
  for(i=0; i < sizeof(int)*8; i++)
  {
    r |= add1(a>>i, b>>i, &c);
    r <<= 1;
  }
  return inv(r);
}

int sub(int a, int b)
{
  return add(a, add(~b, 1));
}

(keeping the same idea the code can be made better, just too tired to do it finer)

ShinTakezou
The OP got that.
quantumSoup
the title being "Subtracting two numbers without using ‘-’ operator" which does not mean implement add/sub bitwise like your shown code, that is just the way you thought it could be done, but there's no clue about why teacher asked you that. Maybe he wants to be sure you understood 2's complement, and ~ being a bitwise operator, makes the whole code bitwise - so this too is taken into account. Moreover, here there's not this simple insight, so the answer can't be "unuseful" (as the down button say)
ShinTakezou
ops - all that code just hides that! - and it is obscure the connection between title and code. This shows the solution is right, provided that add is implemented correctly :) - titles should be more "catch it"
ShinTakezou