Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site jplgodo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!petrus!bellcore!decvax!ittatc!dcdwest!sdcsvax!sdcrdcf!oberon!smeagol!jplgodo!steve From: steve@jplgodo.UUCP (Steve Schlaifer x3171 156/224) Newsgroups: net.lang.c Subject: Re: Right shift vs. divide Message-ID: <537@jplgodo.UUCP> Date: Tue, 24-Dec-85 15:49:49 EST Article-I.D.: jplgodo.537 Posted: Tue Dec 24 15:49:49 1985 Date-Received: Thu, 26-Dec-85 04:09:14 EST References: <974@brl-tgr.ARPA> Organization: Jet Propulsion Labs, Pasadena, CA Lines: 50 > I suspect that most of the people who have been recently flaming in > the right-shift debate don't understand the problem. So...doning > asbestos suit... > > The shift instruction that is normally taken as equivalent to divide is > right shift with sign extension. That is, the sign bit fills in the bits > vacated by the shift. Assuming a 16 bit binary machine and starting with > -15(decimal) or FFF1(hex) and repeatedly shifting right 1 bit gives: > And assuming 2's complement arithmetic... > Shifts Hex Decimal > ----------------------------------- > 0 FFF1 -15 > 1 FFF8 -8 > 2 FFFC -4 > 3 FFFE -2 > 4 FFFF -1 > 5 FFFF -1 > >5 FFFF -1 > > Starting with -16 and dividing by 2, using the standard divide, gives > > Divides Decimal > ----------------------------------- > 0 -15 > 1 -7 > 2 -3 > 3 -1 > 4 0 > >4 0 > > Anyone still think they're the same thing? This optimization only works > if the numbers are known to be non-negative. A Pascal compiler may make > this optimization, becausethe programmer may specify the range for > variables, but not C. > NB: On 1's complement machines like the Univac (Sperry) 1100 mainframes, the sign extended right shift works just like a divide by 2. > Shifts Hex Decimal ----------------------------------- 0 FFF0 -15 1 FFF8 -7 2 FFFC -3 3 FFFE -1 4 FFFF -0 5 FFFF -0 >5 FFFF -0 Steve Schlaifer (jplgodo!steve)