c++ >> and signed numbers?

Jim Wilson wilson@specifixinc.com
Tue Sep 16 03:10:00 GMT 2003


Jerry Quinn wrote:
> According to the c++ spec, operator >> is implementation-defined as to
> what it does with negative numbers.  So, does g++ sign-extend negative
> numbers shifted to the right?  Or is it triplet-specific?

It is undocumented, and thus no result is guaranteed.

The intent is that the right shift sign-extends negative numbers 
(assuming we have a signed type).  We convert it to a sign-extend shift 
in the intermediate language (IL), and we expect that the target will 
emit a sign-extending shift instruction. for this IL construct. 
However, no where do we guarantee this.

In practice, I don't know of any gcc target that doesn't follow this 
convention, and I would be very surprised to see one.
-- 
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com



More information about the Libstdc++ mailing list