T
TTroy
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) Integral promotion vs. Arithmetic Conversion
2) Value Preserving vs. Unsigned Preserving
I've read many, many old posts from c.l.c that referred to those 4
terms constantly.. this is what I think they mean:
Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any operating
is done on them. If an unsigned short has the same bit-size as int,
then the promotion is to unsigned int.
Integral promotion is done as an intermediate step for all common
computations.
Ex:
char a = 9;
unsigned b = 4;
a = b + a; /* a and b both converted to int before addition */
/* the result is then converted back to char by the assignment */
So basically, integral promotion gurantees that all intermediate
computations are dealt with nothing "lower" than an int. Am I right?
-
Artihmetic Promotion
=====================
If the variables involved in an arithmetic operation are all at least
int, then "level" priorities take over.
The priority is : long double, double, float, long, int
If one variable involved in a binary operation is a higher "level" than
the other, the other is promoted to that same level.
After that, the next type of priority is also applied, based on
signed/unsigned.
If one variable is unsigned and the other is signed, the signed is
converted to unsigned.
Once all this is done, we go on with the binary operation to produce
the result. Am I right?
-
Value Preserving
================
I am very confused on this term. Does it mean that, say for example, a
conversion from an unsigned short to an int is done, and the value
stored in the unsigned short is larger than the maximum possible
positive number in an int, then the result is implementation defined.
If the value stored in unsigned short is less than the maximum
posssible positive number in an in, then the exact value will exit in
the int (guaranteed to have msb = 0).
What I don't get about value preserving rules is, whether they are
based on the actual value in variables(run time), or the POSSIBLE
RANGES of the INTEGERS(compile time)?
-
Unsigned Preserving
===================
I have a feeling this is a more strict rule (subset of Value
preserving) that implies 2's compliment. Where actual bit patters are
moved without change, as would be the case for 2's compliment.
-
Many many posts on these subjects have gone through clc's servers, but
they all deal with subsets of what I'm presenting here. If someone
could make a more comprehensive response (or possible add it to the FAQ
website), I think that would be a major help (especially when books
only mention "signed extension".. some don't mention anything.. they
just say "the value is converted"... looked at C89 draft.. little too
advanced for a beginner).
By the way, if one of you clc regulars write a book on C as thorough as
the responses you give, it would force all these phony book authors
into extinction.
[Many of the programmers at my workplace (where I'm an electronics guy)
draw a blank when I ask them about this :-( ]
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) Integral promotion vs. Arithmetic Conversion
2) Value Preserving vs. Unsigned Preserving
I've read many, many old posts from c.l.c that referred to those 4
terms constantly.. this is what I think they mean:
Integral Promotion
===================
All chars and shorts are promoted to int(signed) before any operating
is done on them. If an unsigned short has the same bit-size as int,
then the promotion is to unsigned int.
Integral promotion is done as an intermediate step for all common
computations.
Ex:
char a = 9;
unsigned b = 4;
a = b + a; /* a and b both converted to int before addition */
/* the result is then converted back to char by the assignment */
So basically, integral promotion gurantees that all intermediate
computations are dealt with nothing "lower" than an int. Am I right?
-
Artihmetic Promotion
=====================
If the variables involved in an arithmetic operation are all at least
int, then "level" priorities take over.
The priority is : long double, double, float, long, int
If one variable involved in a binary operation is a higher "level" than
the other, the other is promoted to that same level.
After that, the next type of priority is also applied, based on
signed/unsigned.
If one variable is unsigned and the other is signed, the signed is
converted to unsigned.
Once all this is done, we go on with the binary operation to produce
the result. Am I right?
-
Value Preserving
================
I am very confused on this term. Does it mean that, say for example, a
conversion from an unsigned short to an int is done, and the value
stored in the unsigned short is larger than the maximum possible
positive number in an int, then the result is implementation defined.
If the value stored in unsigned short is less than the maximum
posssible positive number in an in, then the exact value will exit in
the int (guaranteed to have msb = 0).
What I don't get about value preserving rules is, whether they are
based on the actual value in variables(run time), or the POSSIBLE
RANGES of the INTEGERS(compile time)?
-
Unsigned Preserving
===================
I have a feeling this is a more strict rule (subset of Value
preserving) that implies 2's compliment. Where actual bit patters are
moved without change, as would be the case for 2's compliment.
-
Many many posts on these subjects have gone through clc's servers, but
they all deal with subsets of what I'm presenting here. If someone
could make a more comprehensive response (or possible add it to the FAQ
website), I think that would be a major help (especially when books
only mention "signed extension".. some don't mention anything.. they
just say "the value is converted"... looked at C89 draft.. little too
advanced for a beginner).
By the way, if one of you clc regulars write a book on C as thorough as
the responses you give, it would force all these phony book authors
into extinction.
[Many of the programmers at my workplace (where I'm an electronics guy)
draw a blank when I ask them about this :-( ]