Message-ID: <3F24AA4B.589D3482@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: 33 Date: Mon, 28 Jul 2003 04:45:34 GMT NNTP-Posting-Host: 12.81.75.99 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1059367534 12.81.75.99 (Mon, 28 Jul 2003 04:45:34 GMT) NNTP-Posting-Date: Mon, 28 Jul 2003 04:45:34 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 1LU ? I guess the LU means Long Unsigned. Is that now part of standard C? I would of guessed the preprocessor would have handled the math first and the storage later. Thanks Paul Martin Ambuhl wrote: > > Paul Cousoulis wrote: > > Why does this code result in an integer overflow? > > > > unsigned long res; > > > > res = (1<<31)-1; > > Because (1 << 31) is a signed expression. Never use signed integers for > bit twiddling. Here's an example without such overflow. > > #include > > int main(void) > { > unsigned long res; > res = 1LU << 31; > printf("1LU << 31 : %lu\n", res); > res = (1LU << 31)-1; > printf("(1LU << 31)-1 : %lu\n", res); > return 0; > } > > 1LU << 31 : 2147483648 > (1LU << 31)-1 : 2147483647