Is adding two integer and store the result in long int possible?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Linish Francis
    New Member
    • Mar 2015
    • 6

    Is adding two integer and store the result in long int possible?

    In a program which adds two numbers of integer data type and prints the result, numbers greater than 32767 can neither be given as input nor obtained as output.
    for eg:

    if the operends are a and b and the result is c
    neither a nor b nor c can be greater than 32767.now look at the below program

    #include<iostre am.h>
    void main()
    {
    int a,b;
    long int c;
    cin>>a;
    cin>>b;
    c=a+b;
    cout<<c;
    }
    while the program is executed, I give 32767 for a
    and 1 for b.. I know the result is 32768 and since its greater than 32767,it can be stored in c(if c is of integer data type)...but in the above program c is of long int data type which means it has enough and more space to accomodate 32768. Yet the result is -32768...why?
    thanks in anticipation.
    Last edited by Linish Francis; Mar 4 '15, 04:47 PM. Reason: Spelling mistake
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    32768 is the limit of an unsigned 16-bit integer. However, for a signed integer, one bit is needed for the sign bit. That leaves 32767 for the value.

    When you put 32768 in as a value there is a 1 in bit position 15 (counting from 0) and the 15 value bits are all zero.

    The 1 in the sign bit signifies a negative value. Negative values are usually stored in 2's complement. To decode from 2's complement, you reverse the bit values and add 1. Here the 15 data bits that are zero are reversed to 1. This is a decimal value of 32767. Then adding 1 you get 32768. Not forgetting the minus sign you see -32768.

    Comment

    • Linish Francis
      New Member
      • Mar 2015
      • 6

      #3
      I know that...but my question is different. If the data type is integer then the maximum value the memory can,hold is 32767 for which the binary is 011111111111111 1.but in my program the variable c which I use to store the result is of long int data type. In that case numbers greater than 32767 can easily be stored right?... Yet the result is -32768...why????

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        How big is your long int?

        Do a sizeof(int) and a sizeof(long int) and see what the sizes are.

        All that's required is that the long int be the size of an int or may be larger.

        Comment

        • Linish Francis
          New Member
          • Mar 2015
          • 6

          #5
          thanks you so much buddy.... My doubts are very much cleared now...thanks one again

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Actually, long int is required to be at least 32 bits wide.

            The issue is with this instruction:
            Code:
            c=a+b;
            a and b are both ints, so the compiler uses int arithmetic to add them together. However, as explained above, int arithmetic overflows for the values you're using. The compiler assigns the result of the int arithmetic to long int variable c, but by then it is too late.

            You want the compiler to use long int arithmetic. It will do this if at least one operand is long. Here are just a few ways to make that happen:
            Code:
            c=a;
            c+=b;
            Code:
            c=(long)a+b;
            Code:
            c=a+(long)b;
            Code:
            c=(long)a+(long)b;

            Comment

            Working...