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


> Date: Wed, 28 Mar 2001 14:31:32 -0800
> From: Richard Henderson <rth@redhat.com>
> Cc: dewar@gnat.com, gcc@gcc.gnu.org
> Mail-Followup-To: Richard Henderson <rth@redhat.com>,
> 	Geoff Keating <geoffk@geoffk.org>, dewar@gnat.com, gcc@gcc.gnu.org
> Content-Disposition: inline
> User-Agent: Mutt/1.2.5i
> 
> On Wed, Mar 28, 2001 at 01:08:49PM -0800, Geoff Keating wrote:
> > 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.
> 
> Yes, but after the bit manipulations you can't cast that back to
> a signed int without overflowing.  Eg. if you twos-complement
> arithmetic right shift a -1, you'll get a ones-complement min-int.

That's true, you need to actually write something like

unsigned t;
int i;

i = (int)((unsigned)-1 >> 1 & t) - (int)(~((unsigned)-1 >> 1) & t);

-- 
- 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]