avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] odd left-shift behavior, avr-gcc 4.1.2


From: Dean Ferreyra
Subject: Re: [avr-gcc-list] odd left-shift behavior, avr-gcc 4.1.2
Date: Sun, 09 Dec 2007 13:26:11 -0800
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

John Regehr wrote:
> Using avr-gcc 4.1.2, this code:
> 
>   int32_t x = ((uint16_t)0xffffL) << ((uint32_t)1L);
> 
> puts 0xfffe into x, as opposed to 0x1fffe as I would have expected.  Is
> a C compiler not required to promote such that both operands of a shift
> are the same size?

I think the standard calls what you're expecting the "usual arithmetic
conversion", but that conversion is not applied to bitwise shift
operators.  Also, the C99 standard says this about the shift
operators: "The integer promotions are performed on each of the
operands.  The type of the result is that of the promoted left
operand."  I believe this means that the result of the shift in your
example is a uint16_t, so you get 0xfffe which is then promoted to an
int32_t on assignment to x.

Dean





reply via email to

[Prev in Thread] Current Thread [Next in Thread]