From: "News Reader" Newsgroups: comp.os.msdos.djgpp Subject: Re: integer overflow Date: Mon, 28 Jul 2003 16:44:36 +0200 Organization: UTA/netway (Customer) Lines: 45 Message-ID: References: <3F246120 DOT 63C3753C AT worldnet DOT att DOT net> <3F24AA4B DOT 589D3482 AT worldnet DOT att DOT net> NNTP-Posting-Host: pat-ei.lucent.wellcom.at X-Trace: newsreader1.netway.at 1059403633 13408 195.230.174.18 (28 Jul 2003 14:47:13 GMT) X-Complaints-To: abuse AT netway DOT at NNTP-Posting-Date: 28 Jul 2003 14:47:13 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com According to my DJGPP documentation (C-library, HTML) the format string "%U" instead of "%lu" should work, but it doesn't. Any clue why? "Paul Cousoulis" wrote in message news:3F24AA4B DOT 589D3482 AT worldnet DOT att DOT net... > 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