This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: signed/unsigned integer conversion for right shift seems against C99 rule
- From: Tadeus Prastowo <tadeus dot prastowo at unitn dot it>
- To: Peter dot T dot Breuer at gmail dot com
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 6 Feb 2018 16:21:15 +0100
- Subject: Re: signed/unsigned integer conversion for right shift seems against C99 rule
- Authentication-results: sourceware.org; auth=none
- References: <20180206150300.457d70e5@betty.enbd.org>
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