Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!ark From: ark@alice.UucP (Andrew Koenig) Newsgroups: net.lang.c Subject: Re: Divisions with shifts Message-ID: <4789@alice.UUCP> Date: Fri, 10-Jan-86 17:33:21 EST Article-I.D.: alice.4789 Posted: Fri Jan 10 17:33:21 1986 Date-Received: Sat, 11-Jan-86 07:20:17 EST References: <1365@brl-tgr.ARPA> Organization: Bell Labs, Murray Hill Lines: 20 > Just shifting using an arithmetic shift may give the wrong answer, but > you could correct the rounding and still get much faster execution like: > > shiftRightArithmetic register > branchIfPositive label > increment register > label: > > so -23/2 = -23>>1 + 1 = 11101001>>2 + 1 = 11110100 + 1 = 11110101 = -11 > which is the right answer... > > -Miles Yet another example of why this problem is harder than it appears. The algorithm suggested above gives the wrong answer whenever the remainder from the division is 0. Thus (-2>>1)+1 is (11111110>>1)+1 is 11111111+1 is 0. Oh yes, 11101001>>2 + 1 is really 11111101, because addition binds more tightly than shifting.