This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: portable signed right shift


> As the standard states:
> 
> 
> 3 The value of E1 >> E2 is E1 right-shifted E2 bit positions.  If E1 has
>   an unsigned type or if E1 has a signed type and a  nonnegative  value,
>   the  value  of  the  result is the integral part of the quotient of E1
>   divided by the quantity 2 raised to the power E2.  If E1 has a  signed
>   type  and  a  negative  value,  the resulting value is implementation-
>   defined.
> 
> OK, great.  So what is an efficient, portable way to get a signed
> right shift?
>

Cast to unsigned, shift(s) right, set the high bit(s) if orignially negative,
then cast to signed.

Toshi


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]