Message-ID: <3F25F8B9.32167C0@worldnet.att.net> From: Paul Cousoulis X-Mailer: Mozilla 4.78 [en] (Win95; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: integer overflow References: <3F246120 DOT 63C3753C AT worldnet DOT att DOT net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 64 Date: Tue, 29 Jul 2003 04:32:30 GMT NNTP-Posting-Host: 12.81.72.7 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1059453150 12.81.72.7 (Tue, 29 Jul 2003 04:32:30 GMT) NNTP-Posting-Date: Tue, 29 Jul 2003 04:32:30 GMT Organization: AT&T Worldnet To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Thanks, I was trying to get this tgt-edif code which was written for Linux to compile with DJGPP and this code was in it. It Looked okay to me. My reference book for C is 'A Book on C' by Ira Pohl and Al Kelley, Copyright 1998. I guess it's getting a little dated. Thanks Paul News Reader wrote: > > Your compiled code will not result in an overflow. > You are just getting an overflow warning from the > compiler, because the sign bit is affected twice during > the evaluation of your code. It was mentioned in this > thread that DJGPP defaults to signed 32-bit constants. > > In order to prevent compiler warnings you can > change your code as follows: > > unsigned res; > > res=1; res<<=31; res--; // was: (1<<31)-1; > > Other examples which will work are: > > res = (1u<<31)-1; > res = ((unsigned)1<<31)-1; > res = (unsigned)(1<<31)-1; > > Please note that using 'unsigned res' instead of > 'unsigned long res' will not change anything because > DJGPP will produce 32-bit integers in both cases. > > If your intention is to double the size of the variable > you have to use 'long long' in your declaration. > This will get you to 64 bits and your maximum > shift count will therefore be 63 (unsigned). > > Unfortunately this does not help much because > your code will still be evaluated based on 32-bit > signed integer constants - unless you use one of > the following methods: > > long long res; > > res = (1LL<<31)-1; > or > res = ((long long)1<<31)-1; > or > res=1; res<<=31; res--; > > "Paul Cousoulis" wrote in message > news:3F246120 DOT 63C3753C AT worldnet DOT att DOT net... > > > > Why does this code result in an integer overflow? > > > > unsigned long res; > > > > res = (1<<31)-1; > > > > Thanks > > Paul