Right shift vs. divide
Paul Schauble
Schauble at mit-multics.arpa
Mon Dec 23 09:04:16 AEST 1985
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
More information about the Comp.lang.c
mailing list