R
Ralf Damaschke
Branimir Maksimovic said:On 05/26/2011 04:04 PM, Francois Grieu wrote:
[3] some compilers know that, when j is an int,
if (j<1 || j>8)
foo();
can be rewritten as
if ((unsigned)j-1u>7u)
foo();
No, it can't.
Would you please illustrate by an example when that does not
stand?
When j is in range [-8 , 0]
Actually: if(j < -8 || j > 8)
can be rewritten as: if((unsigned)j>8)
Actually not! Consider, say, j = -1. Your first if statement
takes the else-part, your second the then-part - always.
You had had a quarter point when you had argued that the
precision of unsigned might not be larger than that of int.
But:
1: the compiler would know that and not internally rewrite the
expression the way Francis mentioned (for *some* compilers).
2: the vast majority of compilers (if not all, at least all I
know of) have a greater precision for unsigned than for int.
-- Ralf