473,657 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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.
Mar 4 '15 #1
5 4393
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Linish Francis
6 New Member
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????
Mar 5 '15 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Linish Francis
6 New Member
thanks you so much buddy.... My doubts are very much cleared now...thanks one again
Mar 6 '15 #5
donbock
2,426 Recognized Expert Top Contributor
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
1694
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 want to pass a variable containing a query string and URL to a client > side function within the html page that sends it and stores the result in > a variable eg >
8
2962
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 on the mul_a() and pow_a() below comments on style/clarity/portability/obvious efficiency issues are welcome - any better ways to write them without changing the algorithm. 2) Comments on mul_b() and pow_b() below which attempt to optimise them.
19
7263
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
6613
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 necessary, I can create the schema for the 3rd table (C) since it'll be exactly like table A. But I'm more interested in being able to store the resulting set into another table. Thanks.
21
4172
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 possible. The thing I am trying to do is to convert a double value to a string with 8 elements. 8 is fixed because of the files I work with. I will change this 8 character string with the one(8 character string) already in the file and so on. But I...
3
3414
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 is a 64 bit value and I know I am not compiling for 64 bit Windows.
4
8104
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 The nesting asp.net components example. ..........................or do I have to buy this component to do it. :-)
3
11780
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 DECLARE cursor_insert CURSOR FOR SELECT insequenceid FROM temp_ac OPEN cursor_insert
4
8932
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 textboxes and parsing string to Float var txtBx1 = parseFloat(document.getElementById('textField01').value); var txtBx2 = parseFloat(document.getElementById('textField02').value);
4
1918
by: jures | last post by:
... #include <cmath> ... unsigned long long X, j , i, total = 0; cin >> X; for ( i = 0 ; i < 18 ; ++i )
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.