Rajeev
Rajeev

Reputation: 46969

Basic integer explanation in C++

This is a very basic question.Please don't mind but I need to ask this. Adding two integers

int main()
{
     cout<<"Enter a string: ";
     int a,b,c;
     cout<<"Enter a";
     cin>>a;
     cout<<"\nEnter b";
     cin>>b;
     cout<<a<<"\n"<<b<<"\n";
     c= a + b;
     cout <<"\n"<<c ;
     return 0;
}

If I give a = 2147483648 then b automatically takes a value of 4046724. Note that cin will not be prompted and the result c is 7433860

If int is 2^32 and if the first bit is MSB then it becomes 2^31

c= 2^31+2^31

c=2^(31+31)

is this correct?

So how to implement c= a+b for a= 2147483648 and b= 2147483648 and should c be an integer or a double integer?

Upvotes: 0

Views: 217

Answers (3)

bames53
bames53

Reputation: 88225

c= 2^31+2^31

c=2^(31+31)

is this correct?

No, but you're right that the result takes more than 31 bits. In this case the result takes 32 bits (whereas 2^(31+31) would take 62 bits). You're confusing multiplication with addition: 2^31 * 2^31 = 2^(31+31).

Anyway, the basic problem you're asking about dealing with is called overflow. There are a few options. You can detect it and report it as an error, detect it and redo the calculation in such a way as to get the answer, or just use data types that allow you to do the calculation correctly no matter what the input types are.

Signed overflow in C and C++ is technically undefined behavior, so detection consists of figuring out what input values will cause it (because if you do the operation and then look at the result to see if overflow occurred, you may have already triggered undefined behavior and you can't count on anything). Here's a question that goes into some detail on the issue: Detecting signed overflow in C/C++

Alternatively, you can just perform the operation using a data type that won't overflow for any of the input values. For example, if the inputs are ints then the correct result for any pair of ints can be stored in a wider type such as (depending on your implementation) long or long long.

int a, b;
...
long c = (long)a + (long)b;

If int is 32 bits then it can hold any value in the range [-2^31, 2^31-1]. So the smallest value obtainable would be -2^31 + -2^31 which is -2^32. And the largest value obtainable is 2^31 - 1 + 2^31 - 1 which is 2^32 - 2. So you need a type that can hold these values and every value in between. A single extra bit would be sufficient to hold any possible result of addition (a 33-bit integer would hold any integer from [-2^32,2^32-1]).

Or, since double can probably represent every integer you need (a 64-bit IEEE 754 floating point data type can represent integers up to 53 bits exactly) you could do the addition using doubles as well (though adding doubles may be slower than adding longs).

If you have a library that offers arbitrary precision arithmetic you could use that as well.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477504

When you perform any sort of input operation, you must always include an error check! For the stream operator, this could look like this:

int n;
if (!(std::cin  >> n)) { std::cerr << "Error!\n"; std::exit(-1); }
// ... rest of program

If you do this, you'll see that your initial extraction of a already fails, so whatever values are read afterwards are not well defined.

The reason the extraction fails is that the literal token "2147483648" does not represent a value of type int on your platform (it is too large), no different from, say, "1z" or "Hello".

The real danger in programming is to assume silently that an input operation succeeds when often it doesn't. Fail as early and as noisily as possible.

Upvotes: 2

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 71009

The int type is signed and therefor it's maximum value is 2^31-1 = 2147483648 - 1 = 2147483647 Even if you used unsigned integer it's maximum value is 2^32 -1 = a + b - 1 for the values of a and b you give. For the arithmetics you are doing, you should better use "long long", which has maximum value of 2^63-1 and is signed or "unsigned long long" which has a maximum value of 2^64-1 but is unsigned.

Upvotes: 1

Related Questions