7

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;
}
Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80
pranay
  • 2,339
  • 9
  • 35
  • 58
  • 5
    `sub()` is giving you the wrong result because `add()` is wrong. The logic in `sub()` is fine. – NullUserException Aug 07 '10 at 13:49
  • 1
    What's wrong with `-`? What's wrong with `a + b`? – CB Bailey Aug 07 '10 at 13:51
  • This brings back memories too. Our professor wouldn't let us use loops, or `if` statements for that matter. – NullUserException Aug 07 '10 at 13:51
  • @NullUserException: So what if it's homework; it's also tagged as C. `+` and `-` are always available in C. This feels very much like "not a real question" to me. – CB Bailey Aug 07 '10 at 14:04
  • @Charles Bailey: it's not a homework question. I am just learning how to use the bitwise operators – pranay Aug 07 '10 at 14:08
  • I didn't say that it was a homework question, that was NullUserException. You should use bitwise operators when you need to do bitwise operations, typically when you are using values as a mask. If you want to subtract numbers then you should use a subtraction operator, bitwise operations are not the most appropriate solution. – CB Bailey Aug 07 '10 at 14:17

5 Answers5

6

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;
}
Dorian
  • 1,079
  • 11
  • 19
pranay
  • 2,339
  • 9
  • 35
  • 58
2

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
  • 9,432
  • 1
  • 29
  • 39
  • 1
    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 Aug 07 '10 at 16:24
  • 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 Aug 07 '10 at 16:25
2

You also can implement this recursively. In C this might look like:

#include <stdio.h>

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

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

int main(){

    int a = 3;
    int b = 1;

    int sum = add(a, b);
    printf("%i + %i = %i \n", a, b, sum);

    int difference = subtract(a, b);
    printf("%i - %i = %i \n", a, b, difference);

    return 0;
}
Max Kapsecker
  • 484
  • 4
  • 8
1

add method implementation is incorrect. do like this -> A java way of this.

public int add(int a, int b){
  if (b > 0) {
    do {
      a = a & b; //carry
      b = a ^ b;  //addition
      a = a << 1; //carry shift to one bit left
    }while(a != 0);  //exit 
   } else {
     b = a;
   }
   return b;     //addition result
 }

  public int sub(int a, int b){
    return add(a, add(~b, 1)); 
 
  }
umlcat
  • 4,091
  • 3
  • 19
  • 29
Mohammad Adnan
  • 6,527
  • 6
  • 29
  • 47
-4
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.soap.Node;

public class mainone {





 public static void main(String args[]){


 int a=12;
 int b=4;
 Integer  c=new Integer(b);
 String  d=Integer.toString(c);
 String e="-";
 String f=e.concat(d);
 Integer g=Integer.parseInt(f);
 System.out.println(a+g);





 }



 }
Akash Das
  • 49
  • 1
  • 1
  • 2