Beware: Hackers - (nf)

rpw3 at fortune.UUCP rpw3 at fortune.UUCP
Thu Jan 12 02:04:03 AEST 1984


#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 */
+---------------



More information about the Comp.lang.c mailing list