Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83 (MC840302); site kuling.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!mcvax!enea!kuling!thomas From: thomas@kuling.UUCP (Thomas H{meenaho) Newsgroups: net.lang.c Subject: Re: division Message-ID: <873@kuling.UUCP> Date: Thu, 9-Jan-86 23:11:24 EST Article-I.D.: kuling.873 Posted: Thu Jan 9 23:11:24 1986 Date-Received: Sun, 12-Jan-86 06:06:00 EST References: <874@dataioDataio.UUCP> <864@kuling.UUCP> <199@myrias.UUCP> <6263@utzoo.UUCP> Reply-To: thomas@kuling.UUCP (Thomas H{meenaho) Organization: The Royal Inst. of Techn., Stockholm Lines: 26 In article <6263@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) 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. >> >> The first is correct, the second is WRONG. -1/2 is 0 not -1. > [long story about different ways to divide negative numbers] Enough about my comment on arithmetic shifts. I thought the problem was the sign bit which is lost if one uses a logical shift. x x However the idea of adding 2 -1 when dividing a negative number with 2 seems like a win. -- Thomas Hameenaho, Dept. of Computer Science, Uppsala University, Sweden Phone: +46 18 138650 UUCP: thomas@kuling.UUCP (...!{seismo,mcvax}!enea!kuling!thomas)