473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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<iostream.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.
Mar 4 '15 #1
5 4339
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 4 '15 #2
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 0111111111111111.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????
Mar 5 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 5 '15 #4
thanks you so much buddy.... My doubts are very much cleared now...thanks one again
Mar 6 '15 #5
donbock
2,426 Expert 2GB
Actually, long int is required to be at least 32 bits wide.

The issue is with this instruction:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. c=a;
  2. c+=b;
Expand|Select|Wrap|Line Numbers
  1. c=(long)a+b;
Expand|Select|Wrap|Line Numbers
  1. c=a+(long)b;
Expand|Select|Wrap|Line Numbers
  1. c=(long)a+(long)b;
Mar 6 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Adrian | last post by:
Sorry if this appears twice but 6 hours and wasn't up there! "Adrian" <Adrian@nospamhotmail.com.uk> wrote in message news:... > Hi > Is it possible and if so how to do the following? > > I...
8
by: silly | last post by:
/* hello, I have some fairly naive queries here related to optimising code! I know the first answer is 'don't' but leave that to one side for the moment. 1) I'm looking for constructive comments...
19
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
9
by: VMI | last post by:
I have two tables and I want to compare these two tables with a query( ie. "select * from A where B.key = A.key") and the result will be stored in a 3rd table (table C). is this possible? If...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
3
by: Edward Diener | last post by:
I am getting the above warning when compiling a line in a file which assigns an 'intptr_t' result to a 'long' variable. I do not see how intptr_t can possibly be larger than a long unless intptr_t...
4
by: =?Utf-8?B?U2hhbmU=?= | last post by:
Hi all, Is it possible to add controls like buttons, textboxes etc to treeview nodes like this link: http://webui30.componentart.com/treeview/features/nesting_aspnetContent/WebForm1.aspx ...
3
by: KritiGuleria | last post by:
i need to put the result of quey @st into a variable. could anyone please tell me how to do that? select @sys= + columnName+', ' from tab set @sys=left(@sys, len(@sys)-1) select @sys ...
4
by: buntyindia | last post by:
Hi, I have a calculator with seven textBox to add Float numbers upto 2 decimal: I have created following function in js: function total_expenses() { // reteriving all the values from...
4
by: jures | last post by:
... #include <cmath> ... unsigned long long X, j , i, total = 0; cin >> X; for ( i = 0 ; i < 18 ; ++i )
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.