473,395 Members | 1,987 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,395 software developers and data experts.

Arithmetic/Comparison between different integer types


I just want to clarify my understanding of arithmetic and comparison
between two different integer types.
Phase (1): Integer Promotion
----------

All of the following types always get promoted to "signed int":

signed char
signed short

All of the following types get promoted to "signed int" if it has
sufficient range on the platform; otherwise, they go to "unsigned int".

char
unsigned char
unsigned short
Phase (2): Now working with "int" and upwards
----------

Now everything is one of the following types:

signed int
unsigned int
signed long
unsigned long

That leaves us with 10 possibilities. The first four are straight-forward:

(1) signed int + signed int
(2) unsigned int + unsigned int
(3) signed long + signed long
(4) unsigned long + unsigned long

The following involve the same type, but different signedness. Am I right
in thinking that they both become "unsigned int" in (5), and both become
"unsigned long" in (6)?

(5) signed int + unsigned int
(6) signed long + unsigned long
Then there's two different types which have the same signedness. Would I be
right in thinking that they both become signed long in (7), and both become
unsigned long in (8)?

(7) signed int + signed long
(8) unsigned int + unsigned long

Now here's the dirty ones which I'm least sure about. What happens in (9)
and (10)? Are (9) and (10) implementation-specific depending on whether
numeric_limits<long>::digits numeric_limits<int>::digits?

(9) signed int + unsigned long
(10) unsigned int + signed long
--

Frederick Gotham
Jul 7 '06 #1
2 5102

"Frederick Gotham" <fg*******@SPAM.comwrote:
I just want to clarify my understanding of arithmetic and comparison
between two different integer types.
Phase (1): Integer Promotion
----------

All of the following types always get promoted to "signed int":

signed char
signed short

All of the following types get promoted to "signed int" if it has
sufficient range on the platform; otherwise, they go to "unsigned int".

char
unsigned char
unsigned short

Phase (2): Now working with "int" and upwards
----------

Now everything is one of the following types:

signed int
unsigned int
signed long
unsigned long

That leaves us with 10 possibilities. The first four are straight-forward:

(1) signed int + signed int
(2) unsigned int + unsigned int
(3) signed long + signed long
(4) unsigned long + unsigned long

The following involve the same type, but different signedness. Am I right
in thinking that they both become "unsigned int" in (5), and both become
"unsigned long" in (6)?

(5) signed int + unsigned int
(6) signed long + unsigned long
Then there's two different types which have the same signedness. Would I be
right in thinking that they both become signed long in (7), and both become
unsigned long in (8)?

(7) signed int + signed long
(8) unsigned int + unsigned long

Now here's the dirty ones which I'm least sure about. What happens in (9)
and (10)? Are (9) and (10) implementation-specific depending on whether
numeric_limits<long>::digits numeric_limits<int>::digits?

(9) signed int + unsigned long
(10) unsigned int + signed long

The C++ standard, section 5, paragraph 9 reads:
Many binary operators that expect operands of arithmetic or enumeration
type cause conversions and yield result types in a similar way. The purpose
is to yield a common type, which is also the type of the result. This
pattern is called the usual arithmetic conversions, which are defined as
follows:
— If either operand is of type long double, the other shall be converted
to long double.
— Otherwise, if either operand is double, the other shall be converted to
double.
— Otherwise, if either operand is float, the other shall be converted to
float.
— Otherwise, the integral promotions (4.5) shall be performed on both
operands.54)
— Then, if either operand is unsigned long the other shall be converted
to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then
if a long int can represent all the values of an unsigned int, the unsigned
int shall be converted to a long int; otherwise both operands shall be
converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to
unsigned.
[Note: otherwise, the only remaining case is that both operands are int ]
So most of your cases are set in concrete:
In your cases 1,2,3,4 no conversions would occur.
In your case 5, both operands would be converted to unsigned int
In your cases 6, 8, 9, both operands would be converted to unsigned long.
In your case 7, both operands would be converted to signed long

The only case that's implimentation dependent is your case 10.
If long can represent the values, both operands will be converted to long.
However, on most modern 32-bit PCs, long can NOT represent all the values
of unsigned int. Usually the ranges are:
long: -2147483648 to 2147483647
unsigned: 0 to 4294967296
On such machines, in your case 10, both operands will be converted to
unsigned long int, which is usually identical in range to unsigned int.
So any negative values will be converted to very large positive values.
Depending on the application, that could be a disaster or a don't-care.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

Jul 7 '06 #2
Robbie Hatley posted:

<snip explanation>
Thank you, very helpful.

Now I think I'll go over my code looking for any potential booby traps.
--

Frederick Gotham
Jul 7 '06 #3

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

Similar topics

16
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. ...
7
by: Calum | last post by:
Hi, I have a base class called Number that has subclasses Integer and Float. I want to be able to provide a virtual operator- in the base class so that I can subtract one Integer from another...
20
by: asdf | last post by:
I got a warning from gcc for the following code if (nextIs_.size() == NULL) warning: NULL used in arithmetic how can I fix this problem? It seems that I can install a patch for gcc, but I...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
23
by: main() | last post by:
Hi all, I have three newbie questions : 1) According to my understanding, main function can be defined in any of the following two ways, int main(void) int main(int argc,char *argv) How...
27
by: jacob navia | last post by:
As Richard Bos rightly pointed out, I had left in my classification of types the C99 types Complex and boolean. Here is a new classification. Those are not mentioned in the classification of...
26
by: Pietro Cerutti | last post by:
Hi group, I always thought that applying a binary operator such as ==, !=, <= or well defined. Now, I'm passing a program through splint and it says: Dangerous equality comparison involving...
1
by: mmm | last post by:
I wrote the code below to create simple arithmetic sequences that are iter-able I.e., this would basically combine the NUMPY arange(start,end,step) to range(start,end), with step not necessarily...
5
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.