Sujet : Re: What is your opinion about unsigned int u = -2 ?
De : vir.campestris (at) *nospam* invalid.invalid (Vir Campestris)
Groupes : comp.lang.cDate : 12. Aug 2024, 11:47:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9cp7f$38p3u$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 11/08/2024 21:53, James Kuyper wrote:
On 8/11/24 16:08, Vir Campestris wrote:
...
"Converting -1 to an unsigned type always sets all the bits"
>
In any normal twos complement architecture that's the case. But there
are a few oddballs out there where -1 is +1, except that the dedicated
sign bit is set.
There may be hardware where that is true, but a conforming
implementation of C targeting that hardware cannot use the hardware's
result. It must fix up the result produced by the hardware to match the
result required by the C standard.
On 11/08/2024 21:45, Richard Damon wrote:
>
> But, when that -1 value is converted to an unsigned type, that VALUE
> will be adjusted modulo the appropriate power of two.
>
> signed to unsigned conversion works on VALUE, not bit pattern, so is
> invariant with the representation of the negative values.
>
> Yes, in a union with a signed and unsigned, the type punning will let
> you see the representation of the types, but assignment works on values.
Ah, thank you both. It's academic interest only of course!
Andy