Deutsch   English   Français   Italiano  
<20240808193203.00006287@yahoo.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Thu, 8 Aug 2024 19:32:03 +0300
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <20240808193203.00006287@yahoo.com>
References: <v8vlo9$2oc1v$1@dont-email.me>
	<slrnvb7kis.28a.dan@djph.net>
	<v929ah$3u7l7$1@dont-email.me>
	<v92gt1$e1l$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Date: Thu, 08 Aug 2024 18:31:29 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2461de45cb6045aa06c477b66974c4e1";
	logging-data="114453"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19xKf4kqTKIF44cN1uDG+j1BX6Vtf0h7wk="
Cancel-Lock: sha1:DKzz/rHV92uq2SbXe07p4Yfta8I=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
Bytes: 3911

On Thu, 8 Aug 2024 14:23:44 +0100
Bart <bc@freeuk.com> wrote:

> On 08/08/2024 12:14, Thiago Adams wrote:
> > On 07/08/2024 17:00, Dan Purgert wrote: =20
> >> On 2024-08-07, Thiago Adams wrote: =20
> >>> 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?
> >>> [...] =20
> >>
> >> 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)
> >> =20
> >>>
> >>> I also would like to understand better signed and unsigned.
> >>> There is no such think as "signed" or "unsigned" register, right?
> >>> =20
> >>
> >> "Signed" just means the first bit indicates negative.
> >>
> >> So an "unsigned" 8 bit integer will have the 256 values ranging
> >> from
> >>
> >> =C2=A0 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 =3D 0
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (0b00000000)
> >>
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 TO
> >>
> >> =C2=A0 128 + 64 + 32 + 16 + 8 +4 + 2 + 1 =3D 255
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (0b11111111)
> >>
> >>
> >> Whereas a "signed" 8 bit integer will have the 256 values ranging
> >> from
> >>
> >> =C2=A0=C2=A0 (-128) + 0 + 0 + 0 + 0 + 0 + 0 + 0 =3D -128
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (0b10000000)
> >>
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 TO
> >>
> >> =C2=A0=C2=A0 0 + 64 + 32 + 16 + 8 + 4 + 2 + 1 =3D 127
> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (0b01111111)
> >>
> >> At least in two's compliment (but that's the way it's done in C)
> >>
> >> =20
> >>> How about floating point? =20
> >>
> >> 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?)
> >> =20
> >=20
> >=20
> >=20
> > I didn't specify properly , but my question was more about floating=20
> > point registers. I think in this case they have specialized
> > registers. =20
>=20
>=20
> Try godbolt.org. Type in a fragment of code that does different kinds
> of casts (it needs to be well-formed, so inside a function), and see
> what code is produced with different C compilers.
>=20
> Use -O0 so that the code isn't optimised out of existence, and so
> that you can more easily match it to the C source.
>=20
>=20


I'd recommend an opposite - use -O2 so the cast that does nothing
optimized away.

int foo_i2i(int x) { return (int)x; }
int foo_u2i(unsigned x) { return (int)x; }
int foo_b2i(_Bool x) { return (int)x; }
int foo_d2i(double x) { return (int)x; }

etc
https://godbolt.org/z/GWjbcG4GT