Sujet : Re: What is your opinion about unsigned int u = -2 ?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 31. Jul 2024, 22:17:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8e9ks$1oo9l$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
Thiago Adams <
thiago.adams@gmail.com> writes:
What is your opinion about this:
>
unsigned int u1 = -1;
>
Generally -1 is used to get the maximum value.
Is this guaranteed to work?
Yes.
"... the value is converted by repeatedly adding or subtracting
one more than the maximum value that can be represented in the new type
until the value is in the range of the new type." (6.3.1.3p2).
In practice, there's better ways than repeated addition, but that's how
the conversion is defined.
How about this one?
>
unsigned int u2 = -2;
That is guaranteed to produce 1 less than the maximum value.