473,320 Members | 2,073 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,320 software developers and data experts.

Question about integer promotions

On 6.3.1.1 of N1124 we read:

If an int can represent all values of the
original type, the value is converted to
an int; otherwise, it is converted to an
unsigned int.

A few lines above that we read:

The following may be used in an expres-
sion wherever an int or unsigned int may
be used:

So the first quote refers only to objects which can be
represented as an (unsigned) int , right ?

1. So why do integer promotion rules exist ?
After having read http://www.quut.com/c/rat/c2.html#3-2-1-1
I think I can guess at the answer. Traditional
(meaning before the first standard) implementations
had types smaller than int so they had to have *some*
promotion rules for situations where a type
smaller than int encountered across an operator
a type which was int. So the standard simply chose
between the 2 historical integer promotion practices.

2. Why do promotion rules apply when using the ~
operator ?

3. Which promotion rules apply when one of the
operands is (long) long (unsigned) int ?
Spiros Bousbouras

Aug 3 '06 #1
4 1900
sp****@gmail.com wrote:
On 6.3.1.1 of N1124 we read:

If an int can represent all values of the
original type, the value is converted to
an int; otherwise, it is converted to an
unsigned int.

A few lines above that we read:

The following may be used in an expres-
sion wherever an int or unsigned int may
be used:

So the first quote refers only to objects which can be
represented as an (unsigned) int , right ?
Yes.
1. So why do integer promotion rules exist ?
Because processors might not support arithmetic on char/short types.
After having read http://www.quut.com/c/rat/c2.html#3-2-1-1
I think I can guess at the answer. Traditional
(meaning before the first standard) implementations
had types smaller than int so they had to have *some*
promotion rules for situations where a type
smaller than int encountered across an operator
a type which was int. So the standard simply chose
between the 2 historical integer promotion practices.
What you describe now are called the "usual arithmetic conversions"
(6.3.1.8), not the "integer promotions".
2. Why do promotion rules apply when using the ~
operator ?
The rules are consistent for mostly all operators, and for the unary -
operator, processors might not support arithmetic on char/short types.
3. Which promotion rules apply when one of the
operands is (long) long (unsigned) int ?
With binary operators, it depends both on the other argument and their
ranges. With unary operators, there are no promotions.

Aug 3 '06 #2


sp****@gmail.com wrote On 08/03/06 11:58,:
On 6.3.1.1 of N1124 we read:

If an int can represent all values of the
original type, the value is converted to
an int; otherwise, it is converted to an
unsigned int.

A few lines above that we read:

The following may be used in an expres-
sion wherever an int or unsigned int may
be used:

So the first quote refers only to objects which can be
represented as an (unsigned) int , right ?

1. So why do integer promotion rules exist ?
struct {
int x : 7;
int y : 11;
} s = { 1, 2 };
int t = s.x + s.y;

Here we've got an expression that adds a seven-bit
integer and an eleven-bit integer. Flip through the
reference manuals for some of your favorite machines,
and see how many of them have an opcode that adds
operands of those two widths.

No luck? Then how is a C implementation supposed to
evaluate the sum? Software emulation of every operator
whose operands aren't identical and of "lucky" sizes?

C's answer is the integer promotions (more generally,
the "usual arithmetic promotions"), whose goal is to
arrive at a situation the machine can deal with easily.
So each of s.x and s.y gets promoted from its oddball
width to int, then the machine adds the two ints. With
the promotions, C needn't worry about all the possible
combinations of operand types for every operator; instead,
it just needs a way to move each type "up the ladder"
until the operator's operands match. Then C need only
describe how to add two ints, how to divide two doubles,
and so on: it doesn't need to define how to subtract
double from long double, double from double, double from
float, double from unsigned long long, ...
After having read http://www.quut.com/c/rat/c2.html#3-2-1-1
I think I can guess at the answer. Traditional
(meaning before the first standard) implementations
had types smaller than int so they had to have *some*
promotion rules for situations where a type
smaller than int encountered across an operator
a type which was int. So the standard simply chose
between the 2 historical integer promotion practices.

2. Why do promotion rules apply when using the ~
operator ?
Because the Standard says so? How about for consistency's
sake: all the other arithmetic operators[*] promote their
operands with the same set of rules, so why should ~ be weird?
[*] The shift operators are an exception, justified by
the fact that they don't "combine" their operands in the
way + and % and so on do. See the Rationale, where there's
a recap of the debate over whether `1 << 2L' should be
evaluated in int (promoted type of left operand) or in
long (promoted type of shift count).
3. Which promotion rules apply when one of the
operands is (long) long (unsigned) int ?
This is all spelled out in the Standard, and you've been
reading the closely-related N1124. Is there something about
the description in N1124 that is unclear?

--
Er*********@sun.com

Aug 3 '06 #3


Eric Sosman wrote On 08/03/06 12:42,:
>
C's answer is the integer promotions (more generally,
the "usual arithmetic promotions"), [...]
Sorry; "usual arithmetic conversions." A thinko
on my part.

--
Er*********@sun.com

Aug 3 '06 #4
sp****@gmail.com wrote:
>
.... snip ...
>
2. Why do promotion rules apply when using the ~ operator ?
Because, in order to operate on a value, we usually need to pass it
through some sort of arithmetic processor. The default size for
that processor defines the size of an int. Thus the first option
is to convert the value into an int. longs, and/or long longs, may
require specialized processing, while floats and doubles are
usually aimed at a floating point processor.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 5 '06 #5

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

Similar topics

4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
15
by: praeiji | last post by:
Hi, I was wondering something today. The following code : unsigned char a = 200; char b = 200; printf( "%d %d", a, b ); gives :
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
9
by: Fred | last post by:
I'm having terrible trouble trying to work out exactly which of the promotions one reads about in old books are still present in current C - and there seems to be a distinction between promotion...
2
by: Sune | last post by:
Hi all, there are several situations where integer promotion is performed in C and I need to confirm my understanding of it. Let's contain the discussion to workstation/server CPUs of 32/64...
3
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello, I'm having a discussion with someone who sustains that function parameters, when they are smaller than an int (say short or char) are automatically promoted to int before being passed to...
40
by: somenath | last post by:
Hi All, I have one question regarding the behavior of printf function. In page number of 154 in K&R2 in Table 7-1 it is stated that Characters Argument Type :...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.