optimization (was Re: volatile)

Every system needs one terry at wsccs.UUCP
Sat May 14 15:00:44 AEST 1988


In article <375 at m3.mfci.UUCP>, root at mfci.UUCP (SuperUser) writes:
> No, these two programs are not equivalent.  When comparing an unsigned to
> a signed integer, the signed integer is first cast to unsigned (which results
> in no change in the bit pattern), then the comparison is performed.  In fact,
> since octal and hex constants are signed in C, on a machine with 4 byte two's
> complement integers 0xffffffff is equivalent to -1, and people compare these
> signed constants to unsigned values all the same.  Most people probably think
> they're unsigned to being with.  People are often surprised by the fact that
> an expression like (u > -1) is always false when u is unsigned, since the -1
> is first cast to unsigned, whereupon it becomes the largest possible
> unsigned.

I beg to differ.  Not only are programmers not supprised, they expect it
(unless the are so cursed as to be working on a trinary or one's complement
machine).  The programs ARE equivalent to a good optimizer.  In addition,
since the size of int is defined to be the register size, unless the size of
int is 8 bits, the unsigned char will be sign-extended to int to make the
comparison; in this process, everything will be converted to size of int
before the comparison.  The -1 will overflow, losing the sign bit, and you will
come up with something large.  It generally will not equal, unless you are very
lucky.  Then you have badly written code that happens to work.

			terry at wsccs



More information about the Comp.lang.c mailing list