This is the mail archive of the gcc-help@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]
Other format: [Raw text]

Re: signed/unsigned integer conversion for right shift seems against C99 rule


On Tue, Feb 6, 2018 at 4:03 PM, Peter Breuer <peter.t.breuer@gmail.com> wrote:
> int main() {
>   signed   int x = 0x80000005u;
>   unsigned int y = 0x00000002u;
>   signed   int z = x >> y;
>   printf("0x%0x\n", z);
>   return 0;
> }
> % gcc -std=c99 test.c
> test.c: In function 'main':
> test.c:6:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
>    printf("0x%0x\n", z);
>    ^
> test.c:6:3: warning: incompatible implicit declaration of built-in function 'printf'
>
> (I'll live with that warning - just for the purpose of a clean example!)
>
> % ./a.out
> 0xe0000001
>   ^ SIGNED right shift

To quote ISO-IEC 9899:1999 draft
(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) Section
6.5.7 Paragraph 5: The result of E1 >> E2 is E1 right-shifted E2 bit
positions.  [...]  If E1 has a signed type and a negative value, the
resulting value is implementation-defined.
End quote.

So, getting what you got should not surprise you, should it?

> PTB

--
Best regards,
Tadeus


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