This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: portable signed right shift
- To: nbecker at fred dot net
- Subject: Re: portable signed right shift
- From: Toshi Morita <tm2 at best dot com>
- Date: Tue, 27 Mar 2001 13:54:13 -0800 (PST)
- Cc: gcc at gcc dot gnu dot org
> 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