delorie.com/archives/browse.cgi | search |
Message-ID: | <3F24AA4B.589D3482@worldnet.att.net> |
From: | Paul Cousoulis <paulcsouls AT worldnet DOT att DOT net> |
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> <Z6_Ua.375$%S6 DOT 105 AT newsread1 DOT news DOT atl DOT earthlink DOT net> |
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 <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 |