Subtraction without minus sign in C

后端 未结 17 1998
栀梦
栀梦 2020-12-31 10:26

How can I subtract two integers in C without the - operator?

相关标签:
17条回答
  • 2020-12-31 10:46

    This would work using integer overflow:

    #include<limits.h>    
    int subtractWithoutMinusSign(int a, int b){
             return a + (b * (INT_MAX + INT_MAX + 1));
    }
    

    This also works for floats (assuming you make a float version…)

    0 讨论(0)
  • 2020-12-31 10:47

    Not tested. Without using 2's complement:

    #include <stdlib.h>
    #include <stdio.h>
    int sillyNegate(int x) {
       if (x <= 0)
         return abs(x);
       else {
         // setlocale(LC_ALL, "C"); // if necessary.
         char buffer[256];
         snprintf(buffer, 255, "%c%d", 0x2d, x);
         sscanf(buffer, "%d", &x);
         return x;
       }
    }
    

    Assuming the length of an int is much less than 255, and the snprintf/sscanf round-trip won't produce any unspecified behavior (right? right?).

    The subtraction can be computed using a - b == a + (-b).


    Alternative:

    #include <math.h>
    int moreSillyNegate(int x) {
       return x * ilogb(0.5);  // ilogb(0.5) == -1;
    }
    

    0 讨论(0)
  • 2020-12-31 10:47
            int num1, num2, count = 0;
            Console.WriteLine("Enter two numebrs");
            num1 = int.Parse(Console.ReadLine());
            num2 = int.Parse(Console.ReadLine());
            if (num1 < num2)
            {
                num1 = num1 + num2;
                num2 = num1 - num2;
                num1 = num1 - num2;
            }
            for (; num2 < num1; num2++)
            {
                count++;
            }
            Console.WriteLine("The diferrence is " + count);
    
    0 讨论(0)
  • 2020-12-31 10:48

    For the maximum range of any data type , one's complement provide the negative value decreased by 1 to any corresponding value. ex:
    ~1 --------> -2
    ~2---------> -3
    and so on... I will show you this observation using little code snippet

    #include<stdio.h>
    int main()
    {
       int a , b;
       a=10;
       b=~a; // b-----> -11    
       printf("%d\n",a+~b+1);// equivalent to a-b
       return 0;
    }
    

    Output: 0
    Note : This is valid only for the range of data type. means for int data type this rule will be applicable only for the value of range[-2,147,483,648 to 2,147,483,647]. Thankyou .....May this help you

    0 讨论(0)
  • 2020-12-31 10:51
    void main()
    {
    int a=5;
    int b=7;
    
    while(b--)a--;
    printf("sud=%d",a);
    
    }
    
    0 讨论(0)
  • 2020-12-31 10:52
    • + No bit setting
    • + Language independent
    • + Can be adjusted for different number types (int, float, etc)
    • - Almost certainly not your C homework answer (which is likely to be about bits)

    Expand a-b:

    a-b = a + (-b)
        = a + (-1).b
    

    Manufacture -1:

    float:             pi = asin(1.0);
    (with    minusone_flt = sin(3.0/2.0*pi);
    math.h)           or  = cos(pi)
                      or  = log10(0.1)
    complex: minusone_cpx = (0,1)**2; // i squared
    integer: minusone_int = 0; minusone_int--; // or convert one of the floats above
    
    0 讨论(0)
提交回复
热议问题