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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Feb 6, 2018 at 4:03 PM, Peter Breuer <peter.t.breuer@xxxxxxxxx> 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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux