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.arch Subject: Re: Right shift vs. divide Message-ID: <4782@alice.UUCP> Date: Wed, 8-Jan-86 20:37:25 EST Article-I.D.: alice.4782 Posted: Wed Jan 8 20:37:25 1986 Date-Received: Thu, 9-Jan-86 03:50:09 EST References: <3000002@convexs> Organization: Bell Labs, Murray Hill Lines: 31 > arithmetic right shifting is equivalent(numerically) to division if the > algorithm is implemented correctly. just because most machines do it > wrong doesn't mean it doesn't work. the correct algorithm for right > shifting for fixed point is: > > 1)if the operand is positive right shift. > > 2)if the operand is negative, negate the operand before shifting, then > right shift, and then negate the result. > > if you want, try the infamous right shift all 1's (-1). > > the division is round toward zero (ala fortran). > > the MV series from data general perform arithmetic right shifting > correctly Still more evidence that the problem is harder than it looks: the above algorithm fails if the dividend is the most negative number. Consider, for example, a 32-bit machine. Let's divide -2147483648 by 2. The answer should be -1073741824. -2147483648 is 80000000 hex. Negate it and you get integer overflow. If this is masked, most machines will give you 80000000 hex back. Shift 80000000 right 1 bit, giving C0000000. Negate this again, giving 40000000. Thus, the sign gets lost.