This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: portable signed right shift
- To: dewar at gnat dot com
- Subject: Re: portable signed right shift
- From: Geoff Keating <geoffk at geoffk dot org>
- Date: 28 Mar 2001 13:08:49 -0800
- CC: gcc at gcc dot gnu dot org
- References: <20010327223349.E7E7DF289C@nile.gnat.com>
dewar@gnat.com writes:
> >((unsigned)foo >> M) ^ (1 << (N - 1)) - (1 << (N - 1))
>
>
> Note that this gives target dependent results, but if you know you
> are on a twos complement machine with the standard representation
> of signed and unsigned, then you know the result (note that on such
> machines this is likely what the shift operator does anyway :-)
Which part is target-dependent?
I think that if M is between 0 and the number of bits in an
'unsigned', and '1 << (N-1)' does not overflow, then this will produce
the same result on any C implementation.
Note that even on non-twos-complement machines, casting an int to an
unsigned is required to behave the same way, that is (unsigned)-1 is
always the largest possible unsigned value.
--
- Geoffrey Keating <geoffk@geoffk.org>