From owner-freebsd-hackers@FreeBSD.ORG Fri Nov 21 11:30:52 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BC81516A4CE for ; Fri, 21 Nov 2003 11:30:52 -0800 (PST) Received: from episec.com (episec.com [198.78.65.141]) by mx1.FreeBSD.org (Postfix) with SMTP id 0F0C743FE9 for ; Fri, 21 Nov 2003 11:30:52 -0800 (PST) (envelope-from edelkind-freebsd-hackers@episec.com) Received: (qmail 95872 invoked by uid 1024); 21 Nov 2003 19:30:33 -0000 Date: Fri, 21 Nov 2003 14:30:33 -0500 From: ari To: freebsd-hackers@freebsd.org Message-ID: <20031121193033.GM58437@episec.com> Mail-Followup-To: ari , freebsd-hackers@freebsd.org References: <4D7B558499107545BB45044C63822DDE02C094C1@mvebe001.americas.nokia.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4D7B558499107545BB45044C63822DDE02C094C1@mvebe001.americas.nokia.com> Subject: Re: integer and long max/min values X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Nov 2003 19:30:52 -0000 'Tis a bit of a waste of cpu time there. On a two's complement system, which is probably all you'll come by, you can find UINT_MAX by setting an unsigned integer to -1: unsigned int i_max = -1; A slightly more architecturally independent way of finding UINT_MAX would be to set an unsigned integer to the complement of its zero: unsigned int i_max = ~(unsigned int)0; Either way, you can find the positive signed maximum by dividing your result by two (discarding the remainder). Try the following: #include #include ... assert ((unsigned int)-1 == UINT_MAX); assert (~(unsigned int)0 == UINT_MAX); assert (((unsigned int)-1)/2 == INT_MAX); assert ((~(unsigned int)0)/2 == INT_MAX); ... Some may argue against this method, but using an unsigned complement of zero should hold at least the portability of assuming NULL to be false. The *_MAX macros, of course, should still be used whenever possible. ari Vijay.Singh@nokia.com said this stuff: > Write a simple C program to ++ an int or long variable and see when it overflows. > > > -----Original Message----- > > From: owner-freebsd-hackers@freebsd.org > > [mailto:owner-freebsd-hackers@freebsd.org]On Behalf Of ext > > Tim Kientzle > > Sent: Friday, November 21, 2003 12:24 AM > > To: Jay Sern Liew > > Cc: freebsd-hackers@freebsd.org > > Subject: Re: integer and long max/min values > > > > > > Jay Sern Liew wrote: > > > how do I find out the maximum (and minimum) value a long > > and int will hold > > > in C? (before it overflows or underflows) > > > > #include > > > > INT_MAX and INT_MIN are the max/min values for an int > > LONG_MAX and LONG_MIN are the max/min values for long. > > > > Also, see stdint.h, which is defined in C99. > > > > Also, buy a good C reference book. ;-) > > > > Tim Kientzle > > > > _______________________________________________ > > freebsd-hackers@freebsd.org mailing list > > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > > To unsubscribe, send any mail to > > "freebsd-hackers-unsubscribe@freebsd.org" > > > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"