From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a9b0810d3106d9b8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!t19g2000prd.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada Subject: Re: Fun with C Date: Sat, 16 Apr 2011 14:42:41 -0700 (PDT) Organization: http://groups.google.com Message-ID: <1ffeaed4-28d9-4e7c-bf8c-a79db4619bef@t19g2000prd.googlegroups.com> References: <27cf3992-4132-4483-9110-adc7a089cd4a@e8g2000vbz.googlegroups.com> <8762qdyk6y.fsf@ludovic-brenta.org> NNTP-Posting-Host: 75.71.54.181 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1302990161 27426 127.0.0.1 (16 Apr 2011 21:42:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 16 Apr 2011 21:42:41 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: t19g2000prd.googlegroups.com; posting-host=75.71.54.181; posting-account=fZH-XgkAAADP-Rf8L8ppyFIdKUfh90k4 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; GTB6.6),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:19794 Date: 2011-04-16T14:42:41-07:00 List-Id: On Apr 16, 3:12=A0pm, Ludovic Brenta wrote: > "Nasser M. Abbasi" writes: > > > > > > > On 4/16/2011 10:02 AM, George P. wrote: > >> Spend few days chasing bug in embedded C project which can drilled > >> down to this. Consider the following program, and all reasonable men > >> will expect the result to be negative. =A0Not in C, and it is not a > >> compiler bug :-( > > >> And offcorse none of the three comilers I cheched did not bother to > >> issue a warning at least.. > > >> #include > > >> int main() > >> { > >> =A0 =A0 =A0 unsigned n =3D 128; > >> =A0 =A0 =A0 int i =3D -2048; > >> =A0 =A0 =A0 int r; > > >> =A0 =A0 =A0 r =3D i / n; > > >> =A0 =A0 =A0 printf("R =3D %d\n", r); > > >> =A0 =A0 =A0 return 0; > >> } > > > $ gcc -Wall =A0t.c > > $ ./a.out > > R =3D 33554416 > > So R =3D 2**25 - 16, the correct answer is -16, so the actual result is > the correct one except for bit 24, which is 1 when it should be zero. > > Out of curiosity, is this actual result a predictable consequence of the > language definition, or is it undefined behavior? > > -- > Ludovic Brenta.- Hide quoted text - > > - Show quoted text - This is actually predictable. The C compiler implicitly converted the negative value to an unsigned representation of the same bit pattern. The division was performed on that converted value. Note that this wonderful behavior is also expected using C++, because of its compatibility with C. Jim Rogers