This is the mail archive of the gcc@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: GCC 3.1.1


On Fri, Jun 28, 2002 at 11:19:22AM +0100, Rob Taylor wrote:
> 
> Andreas Schwab <schwab@suse.de> writes:
> > Jack Lloyd <lloyd@acm.jhu.edu> writes:
> >
> > |> Basically:
> > |>
> > |> unsigned long long x;
> > |> x = 1 << 32;
> > |>
> > |> results in x == 0 rather than 0x100000000 as the code expects.
> >
> > Actually this is already undefined by itself, and even x == 0 is not
> > guaranteed.
> 
> why? x is an unsigned long long, surely << should be defined for values up to 63
> for this implementation?

x is an unsigned long long, yes, but 1 is not. An unadorned 1 has type
int.  This means that the RHS of the expression above has type int and
will be evaluated as such.  (And if an int has 32 bits or less the
expression won't be well-defined.)  This value will then be assigned to
x. In C the type of a variable on the left side of an assignment does
not influence how the right hand side is evaluated.

If you want the expression to work "as expected" I think
 x = 1LL << 32 
should do the trick.


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se


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