On Wed, 7 May 2008 13:23:50 -0700 (PDT), Tomás Ó hÉilidhe
<toe@[EMAIL PROTECTED]
> wrote in comp.lang.c:
I should have read your reply all the way to the end before my first
response. You made several errors.
> On May 7, 8:20 pm, Eric Sosman <Eric.Sos...@[EMAIL PROTECTED]
> wrote:
>
> > unsigned int ui = (unsigned int)uc << 24;
>
>
> You wouldn't believe how many people do the likes of the following:
>
> char unsigned uc1, uc2;
>
> uc1 = 72;
>
> uc2 = ~uc1;
>
> 1) uc1 gets promoted to a signed int
On many implementations, perhaps including all those you have ever
used, uc1 gets promoted to signed int. There are implementations were
uc1 will get promoted to unsigned int because UCHAR_MAX is greater
than INT_MAX.
> 2) The complement is gotten of this signed int
Of course, on implementations where uc1 is promoted to unsigned int,
the result of the complement is also an unsigned int.
> 3) When the signed int is converted back to unsigned char, the
> behaviour is implementation defined.
This is completely wrong regardless of whether unsigned char promotes
to signed or unsigned int. Assignment of the value of a higher rank
integer type, whether signed or unsigned, to a lesser rank unsigned
integer type is 100% completely defined by the C standard. There is
absolutely no implementation-defined behavior involved.
> There's no problem on a two's complement system, and of course most
> systems are two's complement, but still I'd definitely go with:
>
> uc2 = ~(unsigned)uc1;
On implementations where unsigned char promotes to signed int, the
result of the complement is either a trap representation or
implementation-defined, and that is regardless of the type of
representation for negative signed integers. But if the complement
does not produce undefined behavior by generating a trap
representation, the assignment to unsigned char is always
well-defined.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


|