Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site fortune.UUCP Path: utzoo!linus!decvax!harpo!ihnp4!fortune!rpw3 From: rpw3@fortune.UUCP Newsgroups: net.lang.c Subject: Re: Beware: Hackers - (nf) Message-ID: <2239@fortune.UUCP> Date: Wed, 11-Jan-84 11:04:03 EST Article-I.D.: fortune.2239 Posted: Wed Jan 11 11:04:03 1984 Date-Received: Thu, 12-Jan-84 04:19:52 EST Sender: notes@fortune.UUCP Organization: Fortune Systems, Redwood City, CA Lines: 42 #R:allegra:-218900:fortune:16200018:000:1338 fortune!rpw3 Jan 11 04:59:00 1984 I regret to say, my "fix" of the divide-a-negative-variable-by-a- constant-power-of-two-using-a-shift has a bug in it (OOPS!). As Gary Graunke (Tektronix) rightly points out, the number that should be added to the variable before shifting (if the variable was negative) is the power-of-two-minus-one, not just plain 'ol one. Now you all knew I knew all along, right? Well, I did. (Just ask our local compiler guys.) but (BLUSH!) I didn't proofread my note very well; I was too busy with the fancy "learn from history" quotes. (We will see next time if I learn from history...) Rob Warnock UUCP: {sri-unix,amd70,hpda,harpo,ihnp4,allegra}!fortune!rpw3 DDD: (415)595-8444 USPS: Fortune Systems Corp, 101 Twin Dolphins Drive, Redwood City, CA 94065 +----------------extract from mail--- (also posted to net.lang.c // eventually) | From allegra!tektronix!tekchips!garyg Tue Jan 10 18:47:51 1984 | | The incorrect algorithm is... | | int x; | unsigned int n; | /* replace x/(2^n), n>=0 by ... */ | if(x < 0) | x += 1; | x = x "arith_right_shift" n ; /* where the divisor is 2^n */ | | The correct algorithm is... | | int x; | unsigned int n; | /* replace x/(2^n), n>=0, 2^n-1 <= maxint by ... */ | if(x < 0) | x += 2^n-1; | x = x "arith_right_shift" n ; /* where the divisor is 2^n */ +---------------