Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site dataioDataio.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!vax135!cornell!uw-beaver!uw-june!entropy!dataio!bright From: bright@dataioDataio.UUCP (Walter Bright) Newsgroups: net.lang.c Subject: Re: Two Birds with One Stone Message-ID: <891@dataioDataio.UUCP> Date: Tue, 7-Jan-86 13:17:36 EST Article-I.D.: dataioDa.891 Posted: Tue Jan 7 13:17:36 1986 Date-Received: Wed, 8-Jan-86 06:18:01 EST Reply-To: bright@dataio.UUCP (Walter Bright Organization: Data I/O Corp., Redmond WA Lines: 24 In article <615@brl-sem.ARPA> ron@brl-sem.ARPA (Ron Natalie ) writes: >> >In article <874@dataioDataio.UUCP> bright@dataio.UUCP (Walter Bright writes: >> >>Almost but not quite true. A compiler CANNOT normally replace a divide >> >>by a right-shift if it is an integer divide. This is because a right >> >>shift of a negative integer is not the same as a divide. > >Actually, K&R states that the truncation from an integer divide of negative >numbers is machine dependant -3/2 could correctly be -2 or -1 depending on >the machine. The only guarantee that they make is that > > (a/b)*b + a%b must be equal to a You are correct. However, the point is that on most machines the DIVIDE instruction works one way, and the ARITHMETIC_RIGHT_SHIFT works another way. If the compiler replaces a divide with a right shift, it is WRONG if in doing so it changed the result of the computation. That is, the following two pieces of code better produce the same result: k = -3; i = 2; j = k/i; k = -3; j = k/2;