Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!cbosgd!ihnp4!mhuxn!mhuxr!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!tgr!Schauble@mit-multics.arpa From: Schauble@mit-multics.arpa (Paul Schauble) Newsgroups: net.lang.c Subject: Right shift vs. divide Message-ID: <974@brl-tgr.ARPA> Date: Sun, 22-Dec-85 18:04:16 EST Article-I.D.: brl-tgr.974 Posted: Sun Dec 22 18:04:16 1985 Date-Received: Wed, 25-Dec-85 02:15:52 EST Sender: news@brl-tgr.ARPA Lines: 40 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: 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. BTW, is >> defined to be a sign-extended shift or a zero extended shift and under what circumstances. Paul Schauble @ MIT-Multics