Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-sem.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!brl-sem!ron From: ron@brl-sem.ARPA (Ron Natalie ) Newsgroups: net.lang.c Subject: Re: Two Birds with One Stone Message-ID: <615@brl-sem.ARPA> Date: Fri, 3-Jan-86 16:15:20 EST Article-I.D.: brl-sem.615 Posted: Fri Jan 3 16:15:20 1986 Date-Received: Sun, 5-Jan-86 01:15:06 EST References: <874@dataioDataio.UUCP> <864@kuling.UUCP> <881@dataioDataio.UUCP> Organization: Ballistic Research Lab Lines: 26 > In article <864@kuling.UUCP> thomas@kuling.UUCP (Thomas H{meenaho) 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. > >However most useable processors provide arithmetic shifts which will give > >the right result even if it is a signed divide. > > Ok, try this program: > > main() > { > printf("-3/2 = %d\n-3 >> 1 = %d\n",(-3)/2,(-3) >> 1); > } > > I think you'll find the results are different, even when the compiler > does an arithmetic right shift! (The difference is the direction in > which rounding occurs.) 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 .