473,665 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned long long + int

When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed? What is the
type of the resulting expression? What occurs if the addition overflows
or underflows?

Thanks,

-Peter
Nov 14 '05 #1
12 5438
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:cj******** **@news.apple.c om...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed?
The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.
What is the
type of the resulting expression?
'unsigned long long'
What occurs if the addition overflows
or underflows?


Undefined.

-Mike
Nov 14 '05 #2

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:WI******** ********@newsre ad3.news.pas.ea rthlink.net...
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:cj******** **@news.apple.c om...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed?


The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.
What is the
type of the resulting expression?


'unsigned long long'
What occurs if the addition overflows
or underflows?


Undefined.


Sorry, this is not correct. Upon overflow, unsigned integer
types will 'wrap' to zero. The undefined behavior occurs with
signed integer types.

-Mike
Nov 14 '05 #3
Mike Wahler wrote:
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:cj******** **@news.apple.c om...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed?

The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.


Thanks! How does this promotion occur? Is it guaranteed to convert the
value modulo ULLONG_MAX + 1 (like I hope), or is it an implementation
defined thing?

[...]

-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #4
kal
Peter Ammon <pe*********@ro cketmail.com> wrote in message news:<cj******* ***@news.apple. com>...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed? What is the
type of the resulting expression? What occurs if the addition overflows
or underflows?


Try it on your machine/compiler and let us know.

On of these days I am going to figure out what the following means.

6.3.1.8 Usual arithmetic conversions

1 Many operators that expect operands of arithmetic type cause
conversions and yield result types in a similar way. The purpose
is to determine a common real type for the operands and result.
For the specified operands, each operand is converted, without
change of type domain, to a type whose corresponding real type
is the common real type. Unless explicitly stated otherwise, the
common real type is also the corresponding real type of the result,
whose type domain is the type domain of the operands if they are
the same, and complex otherwise. This pattern is called the usual
arithmetic conversions:

First, if the corresponding real type of either operand is long
double, the other operand is converted, without change of type
domain, to a type whose corresponding real type is long double.

Otherwise, if the corresponding real type of either operand is
double, the other operand is converted, without change of type
domain, to a type whose corresponding real type is double.

Otherwise, if the corresponding real type of either operand is
float, the other operand is converted, without change of type
domain, to a type whose corresponding real type is float.

Otherwise, the integer promotions are performed on both operands.
Then the following rules are applied to the promoted operands:

If both operands have the same type, then no further
conversion is needed.

Otherwise, if both operands have signed integer types or
both have unsigned integer types, the operand with the type
of lesser integer conversion rank is converted to the type
of the operand with greater rank.

Otherwise, if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is
converted to the type of the operand with unsigned integer
type.

Otherwise, if the type of the operand with signed integer
type can represent all of the values of the type of the
operand with unsigned integer type, then the operand with
unsigned integer type is converted to the type of the
operand with signed integer type.

Otherwise, both operands are converted to the unsigned
integer type corresponding to the type of the operand
with signed integer type.
Nov 14 '05 #5
Peter Ammon wrote:
Mike Wahler wrote:
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
When I add an unsigned long long and an int, what type do each
of the values get promoted to before the addition is performed?


The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.


Thanks! How does this promotion occur? Is it guaranteed to
convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
an implementation defined thing?


Yes, guaranteed.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #6
Mike Wahler <mk******@mkwah ler.net> wrote:
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:cj******** **@news.apple.c om...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed?
The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.

^^^^^^^^ -> converted

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #7
kal
CBFalconer <cb********@yah oo.com> wrote in message news:<41******* ********@yahoo. com>...
Peter Ammon wrote:

Mike Wahler wrote:
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message

When I add an unsigned long long and an int, what type do each
of the values get promoted to before the addition is performed?

The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.


Thanks! How does this promotion occur? Is it guaranteed to
convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
an implementation defined thing?


Yes, guaranteed.


Seems so. But I still can't figure out what the standard says.

Say, while converting from long to unsigned long where the value
of long is negative, does this mean the value of unsigned long
is calculated as ULONG_MAX + 1 + <value of long> ?

6.3.1.3 Signed and unsigned integers

2 Otherwise, if the new type is unsigned, the value is converted
by repeatedly adding or subtracting one more than the maximum
value that can be represented in the new type until the value
is in the range of the new type.
Nov 14 '05 #8
kal wrote:

CBFalconer <cb********@yah oo.com> wrote in message news:<41******* ********@yahoo. com>...
Peter Ammon wrote:

Mike Wahler wrote:
> "Peter Ammon" <pe*********@ro cketmail.com> wrote in message
>
>> When I add an unsigned long long and an int, what type do each
>> of the values get promoted to before the addition is performed?
>
> The 'unsigned long long' object is not promoted at all.
> The 'int' object is promoted to 'unsigned long long'.

Thanks! How does this promotion occur? Is it guaranteed to
convert the value modulo ULLONG_MAX + 1 (like I hope), or is it
an implementation defined thing?


Yes, guaranteed.


Seems so. But I still can't figure out what the standard says.

Say, while converting from long to unsigned long where the value
of long is negative, does this mean the value of unsigned long
is calculated as ULONG_MAX + 1 + <value of long> ?

6.3.1.3 Signed and unsigned integers

2 Otherwise, if the new type is unsigned, the value is converted
by repeatedly adding or subtracting one more than the maximum
value that can be represented in the new type until the value
is in the range of the new type.


In most cases it means modulo arithmetic. The verbiage is
necessary to cater to the various allowable integral arithmetic
forms, 2s complement, 1s complement, sign-magnitude.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #9
S.Tobias wrote:
Mike Wahler <mk******@mkwah ler.net> wrote:
"Peter Ammon" <pe*********@ro cketmail.com> wrote in message
news:cj****** ****@news.apple .com...
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed?


The 'unsigned long long' object is not promoted at all.
The 'int' object is promoted to 'unsigned long long'.


^^^^^^^^ -> converted


Objects are not converted, values are. Conversion to higher rank is
called promotion. Like in the Army. :=)
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3597
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
34
16665
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1 - 3;
25
2662
by: amit | last post by:
Hi, What will happen in following scenario.. long a = -2; longlong b = a long c; c = *(ulonglong *)&b; b = c; also when
16
5118
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
9
3943
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
3
36533
by: tutush | last post by:
Hi there, I have problem with importing my C++ code to C#. The c++ looks like these extern _declspec(dllexport) const char* SendAndReceiveBufferedWrp(__int64 hConnection, const char* pchSendBuffer, int iCmdDataMode,unsigned long bufferLength, unsigned long * pulNumBytesRcved) { }
14
12298
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code is right??Or is it a right approch to do that?? unsigned long temp; temp= (unsigned long) buff; temp | =((unsigned long) buff) << 8;
7
5041
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6445
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
13
2038
by: Martin Wells | last post by:
I commonly use the likes of -1 for max unsigned values, or something like -7 to get 6 away from the max value; sort of like: unsigned x = ... if (-7 == x) puts("x is 6 away from its maximum"); Since I rarely use anything smaller than int, this has never been a problem. Obviously though, there'd be a problem if I tried:
0
8438
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
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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
8779
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
8636
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...
1
6187
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
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1761
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.