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


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>


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