Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!necntc!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Expressions compared to zero in *c* Message-ID: <1125@haddock.ISC.COM> Date: Tue, 15-Sep-87 12:40:35 EDT Article-I.D.: haddock.1125 Posted: Tue Sep 15 12:40:35 1987 Date-Received: Thu, 17-Sep-87 04:40:56 EDT References: <374@mcdsun.UUCP> <766@maccs.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 12 Summary: Use unsigned subtract, signed compare In article <766@maccs.UUCP> gordan@maccs.UUCP (Gordan Palameta) writes: >However, if the comparison is done as a - b > 0, everything magically >works thanks to the properties of two's complement arithmetic under >overflows. In fact it works regardless of whether a,b are signed long or >unsigned long (however the result a - b _must_ be considered as a signed long >value, necessitating a cast if a,b are unsigned). A slight quibble here: a and b should be cast into an unsigned type before doing the subtraction. Unsigned arithmetic is *guaranteed* to be modulo 2**N, but signed arithmetic may cause a surprise when overflow occurs. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint