Sujet : Re: how cast works?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 07. Aug 2024, 21:26:12
Autres entêtes
Organisation : None to speak of
Message-ID : <877ccs9j17.fsf@nosuchdomain.example.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
Dan Purgert <
dan@djph.net> writes:
On 2024-08-07, Thiago Adams wrote:
How cast works?
Does it changes the memory?
For instance, from "unsigned int" to "signed char".
Is it just like discarding bytes or something else?
[...]
>
I don't know what happens when you're changing datatype lengths, but if
they're the same length, it's just telling the compiler what the
variable should be treated as (e.g. [8-bit] int to char)
Not necessarily. For example, for an implementation that uses sign and
magnitude or ones'-complement, converting a 32-bit signed value to a
32-bit unsigned type has a well defined result whose representation (bit
pattern) does not match the representation of the argument if it's
negative.
And of course the source and target types don't have to be the same
size. For integers, the conversion is defined in terms of values; it
might be implemented using truncation, zero-extension, sign-extension,
or something else.
C23, not yet published, requires two's complement but still allows for
padding bits.
I also would like to understand better signed and unsigned.
There is no such think as "signed" or "unsigned" register, right?
>
"Signed" just means the first bit indicates negative.
It's more complicated than that.
[...]
How about floating point?
>
Floating point is a huge mess, and has a few variations for encoding;
though I think most C implementations use the one from the IEEE on 1985
(uh, IEEE754, I think?)
Yes, but floating-point conversions (between different floating-point
types or between floating-point and integer) are defined in terms of
values. (double)1 == 1.0, regardless of how that 1.0 is represented.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */