Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!pt.cs.cmu.edu!rochester!udel!princeton!notecnirp!nfs From: nfs@notecnirp.Princeton.EDU (Norbert Schlenker) Newsgroups: comp.arch Subject: Re: Integer Multiply/Divide Message-ID: <22705@princeton.Princeton.EDU> Date: 3 Jan 90 05:34:01 GMT References: <84768@linus.UUCP> <4057@brazos.Rice.edu> <9193@cbmvax.commodore.com> Sender: news@princeton.Princeton.EDU Reply-To: nfs@notecnirp.UUCP (Norbert Schlenker) Organization: Dept. of Computer Science, Princeton University Lines: 12 In article <9193@cbmvax.commodore.com> jesup@cbmvax.commodore.com (Randell Jesup) writes: >In article <4057@brazos.Rice.edu> preston@titan.rice.edu (Preston Briggs) writes: >>Division is slightly more complex, due to sign handling. >>For most languages, it isn't safe to replace a divide by 2^N >>with a shift right (doesn't work for negative numbers). > > Easy solution: x/2^n = (x >> n) + (x & 1 & (bit 31 of x)). Of course, >the >> operation must be "arithmetic" and not "logical" (i.e. sign-extending). This doesn't work. Consider -10/2^3, which results in -2. >Randell Jesup, Keeper of AmigaDos, Commodore Engineering.