Right shift vs. divide

Steve Schlaifer x3171 156/224 steve at jplgodo.UUCP
Wed Dec 25 06:49:49 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:
> 
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)



More information about the Comp.lang.c mailing list