Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: how cast works? Date: Wed, 07 Aug 2024 13:26:12 -0700 Organization: None to speak of Lines: 48 Message-ID: <877ccs9j17.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 07 Aug 2024 22:26:12 +0200 (CEST) Injection-Info: dont-email.me; posting-host="fde1ffae9f6927ccc01afbae751eb89a"; logging-data="3502686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Vit/RFOsU1pVuhA1MmHPA" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:kxc+/F9XCM+FOeqwIsz3s2hGJiE= sha1:5oh75+3uLo0xHuMV/5xDOZ5geD4= Xref: csiph.com comp.lang.c:387384 Dan Purgert 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.com void Void(void) { Void(); } /* The recursive call of the void */