signed/unsigned right shift

Dale Johannesen dalej@apple.com
Fri Mar 12 22:37:00 GMT 2004


On Mar 12, 2004, at 2:24 PM, Andreas Schwab wrote:
> Christian Groessler <cpg@aladdin.de> writes:
>> I have the following program which takes 2 unsigned short values,
>> multiplies them and shifts the result right.
>>
>> This right shift gives a value with 1s bits at the high end.
>
> This is correct.  The promotion rules let unsigned short promote to 
> int,
> since the latter can represent all values of the former.

Correct, the multiplication is done as signed int.  And it is 
implementation
defined whether right shift of a negative signed int does sign fill or 
not.
So both compilers are right; it is only your code that's wrong.

C89 references: 3.3.7, 3.2.1.5, 3.2.1.1.



More information about the Gcc mailing list