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