| delorie.com/archives/browse.cgi | search |
| From: | "News Reader" <nospam AT aol DOT com> |
| 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: | <bg3d1h$d30$1@newsreader1.netway.at> |
| References: | <3F246120 DOT 63C3753C AT worldnet DOT att DOT net> <Z6_Ua.375$%S6 DOT 105 AT newsread1 DOT news DOT atl DOT earthlink 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" <paulcsouls AT worldnet DOT att DOT net> 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 <stdio.h>
> >
> > 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
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |