>Shankar Unni <[EMAIL PROTECTED]> writes:
>
> > Well, what is it supposed to do? Technically, according to the C standard,
> > anyway, this operation has undefined effects, because you are converting a
> > negative number ((signed long) (y - z) * 10) into an unsigned number in
> > order to add to "x". 
>
>Not quite. The conversion from unsigned to signed int *is* well-defined.

For some implementations.


>Chapter and verse: ANSI, 3.2.1.2, lines 23-27:
>
>    "When a signed integer is converted to an unsigned integer with
>    equal or greater size, if the value of the signed integer is
>    nonnegative, its value is unchanged. Otherwise: if the unsigned
>    integer has greater size, the signed integer is first promoted to
>    the signed integer corresponding to the unsigned integer; the
>    value is converted to unsigned by adding to it one greater than
>    the largest number that can be represented in the unsigned integer
>    type. [footnote 28]"

And if the unsigned integer does not have greater size?


>    [footnote 28] In a two's-complement representation, there is no
>    actual change in the bit pattern except filling the high-order
>    bits with copies of the sign bit if the unsigned integer has
>    greater size."
>
>So an expression like "(unsigned int) 1 + -10" is officially
>well-defined and has the value UINT_MAX - 8. 

Assuming two's-complement representation.


>The irony is that an expression such as "(unsigned int) 10 + -10"
>trips over integer overflow and is hence officially undefined, though
>most implementations don't notice integer overflow and will "do what
>you expect" by producing a result of 0.

Yes, I agree. 
The irony is quite thick.

-- Marshall

"The era of big government is over."
           Bill Clinton, State of the Union Address, January 23, 1996
Marshall Clow     Adobe Systems   <mailto:[EMAIL PROTECTED]>

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to