Path: ...!news.misty.com!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Dan Purgert Newsgroups: comp.lang.c Subject: Re: how cast works? Date: Wed, 7 Aug 2024 20:00:28 -0000 (UTC) Organization: A noiseless patient Spider Lines: 53 Message-ID: References: Injection-Date: Wed, 07 Aug 2024 22:00:28 +0200 (CEST) Injection-Info: dont-email.me; posting-host="117cd8aa247bb0ea11d169c77eaace85"; logging-data="3505212"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lAykx0vUlIDNmxSPcA6gY2vSHNtW78t8=" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:mg2IMxQLHY/meS6Gag/bjjSgaxw= Bytes: 2281 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) > > 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. So an "unsigned" 8 bit integer will have the 256 values ranging from 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 = 0 (0b00000000) TO 128 + 64 + 32 + 16 + 8 +4 + 2 + 1 = 255 (0b11111111) Whereas a "signed" 8 bit integer will have the 256 values ranging from (-128) + 0 + 0 + 0 + 0 + 0 + 0 + 0 = -128 (0b10000000) TO 0 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127 (0b01111111) At least in two's compliment (but that's the way it's done in C) > 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?) -- |_|O|_| |_|_|O| Github: https://github.com/dpurgert |O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860